Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from and writing to file in The Qt Resource System (qt 5.0.2)

Tags:

c++

qt

qt5

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();
}
like image 541
Haimanot Avatar asked Apr 27 '13 09:04

Haimanot


People also ask

What is a Qt Resource file?

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.

What is the Qt resource system?

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.

What does the CreateFile () function do in QT 6?

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.

How to use qfile in qtextstream?

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.

Why can't qfile read and write device files?

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.


1 Answers

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
like image 165
Jask Avatar answered Oct 16 '22 07:10

Jask