c++windowsqtpathqt-resource

How to convert Qt file path from resource to absolute path?


I need to receive the absolute file path like

C:/Users/Dima/YandexDisk/_Application/WeldAnalysis/ExperimentDefaults.xlsx

from path of QRC file like

:/Data/ExperimentDefaults.xlsx.

How can I do this?


Solution

  • Resources are packed all together into the QRC file, they are not exposed to the filesystem, thus they cannot be converted to a standard path; it is like accessing a file inside a zip file.

    What are you willing to do with such file path? QFile can deal with resource paths (":/...") and open such files, but other file handling APIs don't (such as C's fopen or C++'s std::ifstream). Also, and for the same reason, non-Qt application won't be able to open such files if passed as a parameter.

    Options you have include:

    Only be aware that when tempDir is destroyed, the temporary directory is also deleted, therefore your file. You can avoid this by disabling the auto-remove feature: tempDir.setAutoRemove(false) and manually deleting the temporary directory when you finish with the extracted file.