Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

        QString path = QDir::currentPath();
        path.append("/acc.xml");
        QFile file(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();
like image 674
sdadffdfd Avatar asked Jan 19 '11 04:01

sdadffdfd


2 Answers

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

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

like image 172
Johnny Avatar answered Nov 14 '22 21:11

Johnny


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.

like image 29
Donotalo Avatar answered Nov 14 '22 22:11

Donotalo