C++ Qt如何打开当前目录中的文件?或者这有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4731736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:17:54  来源:igfitidea点击:

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

c++qtqt-creatorqmake

提问by sdadffdfd

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

我正在尝试在可执行文件的当前位置打开一个 xml 文件

        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

  • 当我从 Qt Creator 运行它时,一切正常。currentPath()返回可执行文件文件夹的路径

  • 当我转到project-build-desktop/文件夹并尝试手动运行它时currentPath()返回/home/user/Documents

EDIT

编辑

also tried with same results:

也尝试过相同的结果:

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

回答by Johnny

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

尝试使用 QCoreApplication::applicationDirPath() 而不是 QDir::currentPath()。

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

有关详细信息,请参阅http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath

回答by Donotalo

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.

检查 的返回值QDir::currentPath()。我认为当您从 Qt Creator 运行时,它会返回项目文件 (*.pro) 所在的路径。当你从外面运行时,你会得到二进制文件的路径。

Edit

编辑

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

我从来没有用过 Linux。但是,您可以尝试以下其他功能/组合QDir

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

etc.

等等。

回答by Dat Chu

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

要打开当前目录中的文件,只需调用 QFile 构造函数

I tested this on my Linux machine and it works

我在我的 Linux 机器上测试了这个,它可以工作

#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.

我运行 ./TestQFile ,如果当前目录中有 test.xml ,它就可以工作。

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.

更新 2:在 QtCreator 的项目面板下,有一个用于工作目录的字段。如果您通过 QtCreator 运行它,这是由 QDir::currentPath() 返回的目录。

回答by sakisk

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.

我在寻找类似解决方案时发现了这个讨论。我认为打开具有固定名称的外部文件(并且不涉及对话框和用户)的最便携方式是使用Resource System

In my case I created a new resource file with the prefix /configand 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.

就我而言,我创建了一个带有前缀的新资源文件/config并添加了该文件(称为settings.xml)。在代码内部,我根本不需要使用任何路径函数。我改用资源系统。因此,像这样的调用QFile file(":/config/settings.xml")工作正常。在 Windows 上使用 QT creator 2.0.1 和 QT 4.7。