Qfile open returns false

Qfile open returns false

本文关键字:false returns open Qfile      更新时间:2023-10-16

我将Content.xml保存在我的app.exe存在的文件夹中。

但是open call返回false ?

文件的确切路径应该是什么?

我不想给出文件的完整路径,但希望我的代码是路径独立的"意味着我想要读取的文件应该在我的exe所在的文件夹中"

#define FILE_NAME "Content.xml"
QString xmlFileName(FILE_NAME);
xmlFile.setFileName(xmlFileName);
if ( ! xmlFile.open(QIODevice::ReadOnly|QIODevice::Text) )
{
    QMessageBox* msgBox = new QMessageBox();
    msgBox->setText("File Not Found !!");
    msgBox->setWindowFlags(Qt::WindowStaysOnTopHint);
    msgBox->exec();
}

尝试使用完整路径打开文件,如:

xmlFile.setFileName(QCoreApplication::applicationDirPath() + QLatin1Char('/') + xmlFileName);

如果您使用Qt Creator中的运行按钮运行代码,则Content.xml需要与您的代码在同一文件夹中