Qt如何在当前dir中打开一个文件?或这有什么问题? [英] Qt how to open a file in current dir ? or what's wrong with this?

查看:451
本文介绍了Qt如何在当前dir中打开一个文件?或这有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在可执行文件的当前位置打开一个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 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();
    

    解决方案

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

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

    这篇关于Qt如何在当前dir中打开一个文件?或这有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆