【问题标题】:Reading from and writing to file in The Qt Resource System (qt 5.0.2)在 Qt 资源系统 (qt 5.0.2) 中读取和写入文件
【发布时间】:2013-04-27 09:45:59
【问题描述】:

我有下面的代码。我正在使用 Qt_5_0_2_MSVC2012_64bit-Release。我无法读取文件。我收到“无法打开文件进行读取”的调试错误消息。资源文件对我来说有些问题。知道如何解决吗?谢谢!

#include <QCoreApplication>
#include <QFile>
#include <QString>
#include <QDebug>
#include <QTextStream>
#include <QResource>
#include <QIODevice>


void Read(QString Filename){
    QFile mFile(Filename);

    if(!mFile.open(QFile::ReadOnly | QFile::Text)){
        qDebug() << "could not open file for read";
        return;
    }

        QTextStream in(&mFile);
        QString mText = in.readAll();

        qDebug() << mText;

        mFile.close();



}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Read(":/MyFiles/myfile.txt");
    return a.exec();
}

【问题讨论】:

  • 1) 将 mFile.errorString() 添加到您的调试输出中以了解有关确切原因的更多信息 2) 添加 qrc 文件的内容
  • 请注意,Qt资源编译后可以读取但不能写入。

标签: c++ qt qt5


【解决方案1】:

我有同样的问题。错误字符串是“未知错误”。
解决方案是从 @gatto 的答案中添加 INCLUDEPATH += . 并从菜单运行命令:

1. Build -> Clean all
2. Build -> Run qmake
3. Build -> Rebuild All

【讨论】:

    【解决方案2】:

    test.pro:

    TEMPLATE = app
    TARGET = test
    INCLUDEPATH += .
    
    # Input
    SOURCES += main.cpp
    RESOURCES += test.qrc
    

    test.qrc:

    <!DOCTYPE RCC><RCC version="1.0">
     <qresource>
        <file>MyFiles/myfile.txt</file>
     </qresource>
     </RCC>
    

    main.cpp 来自您的问题。工作正常。

    也就是说,如果您仍然有问题,您应该发布有错误的最小 Qt 项目(包括 .pro 和 .qrc 文件)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      相关资源
      最近更新 更多