Search code examples
qtqt5qresource

Qt filename to bundled resource


I have built a package in Qt that uses the external gdal library to query a raster file. I want to bundle the raster with my application and had hoped to do that in a qrc resource file.

Problem is that gdal wants a filename:-

const char *pzFileName = nullptr;
GDALAllRegister();
.
.
pzFileName = "/opt/mapping-data/SRTM/my_dem.tif";
testDataSet =  (GDALDataset *) GDALOpen(pzFileName,GA_ReadOnly);

Is there a way I can bundle this file with my Qt app such that the filename is known on every platform? As gdal will have no idea of a "qrc/..." type url.


Solution

  • I would copy out the resource to a temporary file each time at the start of your application. GDAL seems to rely heavily on files and file names. You might use something like this:

    QFile f(":/resources/file.txt");
    QTemporaryFile::createNativeFile(f); // Returns a pointer to a temporary file