Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Copy Qt resource file into filesystem



  • Hi all,
    I want to copy a resource file into my current directory containing the executable file. The copy file should be in folder abc (not existed) inside my current directory.
    I am using QFile::copy() methods like this:

    #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;
    }
    

    The problem is that, when I run the executable file, it returns failed.
    I am expecting that it will create new directory abc inside my current folder then copy the resource file into that.
    Can anyone help me?

    Thanks and best regards.
    Thanh C. Tran


  • Lifetime Qt Champion

    @thanhxp said in Copy QT resource file into filesystem:

    I am expecting that it will create new directory abc inside my current folder

    That is a wrong assumption: copy() will not create any directories. You have to create it by yourself, see QDir (http://doc.qt.io/qt-5.7/qdir.html#mkdir).



  • @jsulm
    Thank you very much. I will redesign my program with QDir.
    Best regards.

    Thanh C. Tran


Log in to reply