Qt如何在当前dir中打开一个文件?或这有什么问题? [英] Qt how to open a file in current dir ? or what's wrong with this?
问题描述
我尝试在可执行文件的当前位置打开一个xml文件
QString path = QDir :: currentPath ();
path.append(/ acc.xml);
QFile文件(路径);
if(!file.open(QIODevice :: ReadOnly))
{
insertItem(IO ERR);
}
-
创作者,一切工作。
currentPath()
返回可执行文件夹的路径 -
> project / build / desktop / 文件夹,并尝试手动运行
currentPath()
也尝试了相同的结果:
Qt :: current
Qt :: current()。absolutePath();
解决方案尝试使用QCoreApplication :: applicationDirPath QDir :: currentPath()。
有关详细信息,请参阅 http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath
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 folderWhen I go to
project-build-desktop/
folder and try to run it manuallycurrentPath()
returns/home/user/Documents
EDIT
also tried with same results:
Qt::current().path(); Qt::current().absolutePath();
解决方案Try to use QCoreApplication::applicationDirPath() instead of QDir::currentPath().
For details see http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath
这篇关于Qt如何在当前dir中打开一个文件?或这有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!