c++qtqt5qfileqresource

Qt: empty content of opened QFile for a .txt file from the project resources


I tried this in my project in mainwindow.cpp:

QString dir = ":/nodesDir/nodesDir/";
QFile baseFile(dir + "allNodeNames.txt");
qDebug() << baseFile.exists(); // true
qDebug() << baseFile.readAll(); // ""

but it is wrong, the content of the file is

plusOperator

Why does it say, that there would be nothing written in the file? Or What did I miss in my code?` Thanks for answers!


Solution

  • In order to read the file you need to open it for it we use open () and we indicate the way we want it to open. We must also bear in mind that the files stored in the resources are read only, so they can not be modified.

    QString dir = ":/nodesDir/nodesDir/";
    QFile baseFile(dir + "allNodeNames.txt");
    qDebug() << baseFile.exists(); // true
    qDebug()<< baseFile.open(QFile::ReadOnly);
    qDebug() << baseFile.readAll(); // ""
    

    Output:

    true
    true
    "plusOperator"