Using a text file as resource of the project

  • I'm trying to access a text file using a function in c++ to read this but I don't know how access this text file.
    The text file are in the project folder and was added into the project as a resource. I already tryed put the full url using the prefix but still not working.

    the .qrc file

        <qresource prefix="/NN">
            <file alias="dumped">dumped.nnet</file>

    P.S. I'm trying this on a Android project.

  • Hi,

    Sample code for fetching the data from resource file
    I have used prefix and added sampleText.txt

    Then using QFile class to read the data from the text file.

    void RessourceFile::getData()

    QString data;
    QString fileName(":/new/prefix1/sampleText.txt");
    QFile file(fileName);
    if(! {
        qDebug()<<"filenot opened"<<endl;
        qDebug()<<"file opened"<<endl;
        data = file.readAll();


    Which worked in Desktop and android platforms.
    Hope this helps you.


  • This post is deleted!

  • I already have a function to read the file and its works but only when I put the file directly into the memory by usb and pass the path to the function. What I need is a way to do this using the file inside the project but when I try pass to the function this :
    the function can't find the file.

    my funcion:

    keras::KerasModel m("/sdcard/Documents/dumped.nnet");

    Don't work
    keras::KerasModel m(":/NN/dumped" );

  • Lifetime Qt Champion

    Inside KerasModel where you try to read the file, do you use
    QFile to read it?
    Other file function ( std c++) do not understand ":/" syntax.

  • @mrjj No, I'not using QFile is a std function.
    Is there any way to do this in std or I have to change to QFile?

  • Lifetime Qt Champion

    You must use QFile.
    the text file is actually embedded into the exe file and only
    QFile can read it from there.
    Other file classes simply do not know about resources and hence QFile must be used.

  • Ok thanks for your help!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.