How to access Qt resource data from non-Qt functions

后端 未结 3 1473
Happy的楠姐
Happy的楠姐 2021-01-02 04:19

As I understand it, the way to packages non-code resources such as data files in a Qt app is using the resource system. However, what if I want to access a resource using a

3条回答
  •  迷失自我
    2021-01-02 04:50

    You can copy the resource file into a temporary folder. To do this, use a QTemporaryDir which creates a temporary folder and deletes it automatically when the program is finished. To access the path of that folder, use the QTemporaryDir::path() method. Here is an example of how you can use it:

    #include     //You need to include this header
    QTemporaryDir temporaryDir;
    //Copy the resource file into the temporary folder
    QFile::copy(":/exampleprefix/examplefile.txt", temporaryDir.path() + "/examplefile.txt");
    //Read the file
    std::ifstream fileStream(QString(temporaryDir.path() + "/examplefile.txt").toLatin1().data());
    //etc
    

提交回复
热议问题