I have the code below. I am using Qt_5_0_2_MSVC2012_64bit-Release. I am not able to read the file. I get the debug error message of "Cannot open file for reading".There is some problem for me with resource files. Any idea how I can fix it? 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();
}
The Qt resource system is a platform-independent mechanism for shipping resource files in an application. Use it if your application always needs a certain set of files (like icons, translation files, images), and you don't want to use system-specific means to package and locate these resources.
The Qt resource system is a platform-independent mechanism for shipping resource files in an application. Use it if your application always needs a certain set of files (like icons, translation files, images), and you don't want to use system-specific means to package and locate these resources.
This function was introduced in Qt 6.0. Constructs a new file object with the given parent to represent the file with the specified name. Constructs a new file object with the given parent. Constructs a new file object to represent the file with the given name. This function was introduced in Qt 6.0.
A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream. The file name is usually passed in the constructor, but it can be set at any time using setFileName (). QFile expects the file separator to be '/' regardless of operating system.
This implementation detail means that QFile is not suitable for reading and writing certain types of files, such as device files on Unix platforms. File permissions are handled differently on Unix-like systems and Windows. In a non writable directory on Unix-like systems, files cannot be created.
I had same problem. The Error string was "Unknown error".
Solution was to add INCLUDEPATH += .
from @gatto's answer and run commands from menu:
1. Build -> Clean all
2. Build -> Run qmake
3. Build -> Rebuild All
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With