How to extract a resource from qrc (Solved)



  • Hi,

    I need extract 1 file on first run of my desktop application. My resource is in a qrc.

    How do it?

    Thanks.



  • QFile::copy(":/path/to/qrc", "path/to/copy");
    copies the file from qrc to directory you specify. You can use other methods of QFile to load the file in memory.



  • Resources are completely transparent to the application and they might be treated as regular files, for example for copying (see previous post). The only restriction is that they are read-only, both the files itself (you cannot modify them) and the directory strucuture they are contained in (you cannot add or remove files to or from a resource collection).



  • Qt is wonderful -

    My simple question have a simple answer.

    Thanks!



  • @pkj__
    I did as you said but I cannot write a QT resource file to a directory-structure-path.
    Here is my code:

    #include <QCoreApplication>
    #include <QFile>
    #include <QString>
    #include <QDebug>
    #include <QTextStream>
    #include <iostream>
    
    using namespace std;
    
    void read(QString filename)
    {
        QFile file(filename);
        if(!file.open(QFile::ReadOnly |
                      QFile::Text))
        {
            qDebug() << " Could not open the file for reading";
            return;
        }
    
        QTextStream in(&file);
        QString myText = in.readAll();
    
        // put QString into qDebug stream
        qDebug() << myText;
    
        file.close();
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        read(":/resources/hello.pro");
        bool status = QFile::copy(":/resources/hello.pro" , "~/abc/hel.pro");
        if(status) {
                cout << "Success" << endl;
            } else {
                cout << "Failed" << endl;
            }
    
    
        return 0;
    }
    

    Could you help me why I am getting Failed. I am expecting that I will create a new folder named abc inside the current directory and copy the resource file into that.
    Thank and best regards.

    Thanh C. Tran


  • Qt Champions 2016

    @thanhxp
    Hi
    Copy will not make a new folder
    You must do that yourself
    QDir().mkdir("~/abc");

    Update:
    https://forum.qt.io/topic/72915/copy-qt-resource-file-into-filesystem/2



  • @mrjj
    Thanks, problem solved.
    Best regards,
    Thanh C. Tran


Log in to reply
 

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