Reading and writing to a file in the Qt Resource System (qt 5.0.2)

I have the code below. I am using Qt_5_0_2_MSVC2012_64bit-Release. I can not read the file. I get a debug error "Can't open file for reading." For me there are some problems with resource files. Any idea how I can fix this? Thanks!

#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(); } 
+8
c ++ qt qt5
source share
2 answers

I had the same problem. The string "Error" was "Unknown error."
The solution was to add INCLUDEPATH += . from @gatto's answer and run the commands from the menu:

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

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 is your question. It works great.

However, if you still have a problem, you should post a minimal Qt project (including .pro and .qrc files) that has an error.

+2
source share

All Articles