0
我需要打開配置文件。配置文件位置是exe文件所在的目錄。基本上,我怎麼能得到這個位置?如何確定使用Qt的可執行文件的目錄?
我試圖使用QDir,但是當前代碼返回錯誤,當文件沒有打開。
QString cfg_name = QDir::currentPath() + "config.cfg";
QFile File(cfg_name);
if (File.open(QIODevice::ReadOnly))
{
QTextStream in(&File);
int elementId;
while (!in.atEnd())
{
QString line = in.readLine();
filename[elementId] = line;
elementId++;
}
}
else
{
QMessageBox msgBox;
msgBox.setText("Can't open configuration file!");
msgBox.exec();
}
File.close();
它仍然返回我的消息框,什麼時候能」 t打開文件。 –
我在代碼中發現錯誤,對不起。 'QString cfg_name = QDir :: currentPath()+「/config.cfg」;' –
@ЦунскийНикита謝謝,糾正了我的答案。 – jotik