开发者

Qt how to open a file in current dir ? or what's wrong with this?

开发者 https://www.devze.com 2023-02-05 04:47 出处:网络
I\'m trying to open an xml file in the current location of the executable QString path = QDir::currentPath();

I'm trying to open an xml file in the current location of the executable

        QString path = QDir::currentPath();
        path.append("/acc.xml");
        QFile f开发者_如何学Goile(path);

        if(!file.open(QIODevice::ReadOnly))
        {
            insertItem("IO ERR");
        }
  • When I run it from Qt creator, everything works. currentPath() returns the path to the executable's folder

  • When I go to project-build-desktop/ folder and try to run it manually currentPath() returns /home/user/Documents

EDIT

also tried with same results:

Qt::current().path();
Qt::current().absolutePath();


Try to use QCoreApplication::applicationDirPath() instead of QDir::currentPath().

For details see http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath


Check the returned value of QDir::currentPath(). I think when you run from Qt Creator, it returns the path where the project file (*.pro) is located. When you run from outside, you get path of the binary.

Edit

I never worked with Linux. However, you can try other functions/combinations from QDir:

  • QDir::current().path()
  • QDir::current().absolutePath()

etc.


To open a file in the current directory, you simply call QFile constructor

I tested this on my Linux machine and it works

#include <QtCore>

int main(int argc, char** argv){
    QFile some_file("test.xml");
    if(!some_file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "Unable to open file";
    } else {
        qDebug() << "File open successfully";
    }
    exit(-1);
}

I run ./TestQFile and if there is a test.xml in the current directory, it works.

UPDATE: I notice that the wording of your question says that you want the file in the same directory as the executable, this can be done as follow:

// Getting directory of the executable
QFileInfo exec_fileinfo(argv[0]);
qDebug() << "Executable is in" << exec_fileinfo.absolutePath();

UPDATE 2: Under the project panel of QtCreator, there is a field for Working Directory. This is the directory that is returned by QDir::currentPath() if you are running it via QtCreator.


I found this discussion while searching for a similar solution. I think that the most portable way of opening an external file that has a fixed name (and no dialogs and the user are involved) is to use the Resource System.

In my case I created a new resource file with the prefix /config and added the file (called settings.xml). Inside the code, I don't need to use any path functions at all. I use the resource system instead. Thus a call like QFile file(":/config/settings.xml") works fine. Using QT creator 2.0.1 and QT 4.7 on Windows.


My tip is this : name of the file + the absolute path to it before opening it. For that I call the method applicationDirPath on our QCoreApplication object (the a object in my case), which returns us the path to the executable of the application. No need going through the argv array. That's it and works

 int main(int argc, char *argv[]) {
   QCoreApplication a(argc, argv);
   const QString fileName = "someJsonFile.JSON";
   const QString currentExecDir = a.applicationDirPath();
   QFile myFile(currentExecDir + '/' + fileName);
   if (myFile.open(QIODevice::ReadOnly)) {
    qDebug()<< "The file "<< fileName<< " has been opened successfully";
   }
   else {
     qDebug()<< "Failed to open file "<< fileName<< " in "<< currentExecDir;
   }
   return a.exec();
}

NOTE Please see the Qt docs for the applicationDirPath method, because it assumes that the current directory has not been changed by the application

0

精彩评论

暂无评论...
验证码 换一张
取 消