Search code examples
c++qtqt-creatorqmake

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();

Solution

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

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