NicoTine
NicoTine

Reputation: 71

C++ / Qt - Cannot Open .txt from qrc file

I tryed a lot of things but they didnt work.

I'm using Qt 5.9.1 and i wouldlike to open a .txt file (which is in a qrc file) in a QFile variable, like this :

QFile file(":/txt/config");

I also tried to use

QFile file("qrc:/txt/config"); 

Here's the qrc file (summarized) :

<qresource prefix="/txt">
    <file alias="config">resources/files/config.txt</file>
</qresource>

My .pro does have INCLUDEPATH += .

I have already tryed to :

Build -> Clean all
Build -> Run qmake
Build -> Build all

And it changed nothing, at every launch, I have this output :

QIODevice::read (QFile, ":/txt/config"): device not open

The path in .qrc is correct, QtCreator find the file when i browse the directories and open it in editor like a normal text file

Thanks for your help, and sorry for my english ... (and the edit function does not allow me to add hello everyone on the top, so i say it here :) )

Upvotes: 2

Views: 4430

Answers (1)

LeBlue
LeBlue

Reputation: 635

You cannot open a resource file for writing, as the content is embedded in the application binary. You have to open it readonly:

QFile file(":/txt/config");
if(!file.open(QIODevice::ReadOnly)) {
    qDebug() << "error: " << file.errorString();
}

Upvotes: 8

Related Questions