PDA

View Full Version : Create files required by application from resource files?



davethomaspilot
5th September 2022, 01:31
My application needs some files to executed properly. The files are needed for initialization of an embedded interpreter and the library functions only support passing a filename, not an open file descriptor.

I can have the files live in a known directory, but they could get accidently deleted by accident. I'm wondering if there's a good way to create the files from Qt resources?

I can have a trivial Qt method that copies the files at startup from their resource definitions into temporary files and use the copies.

Is there a better way?

d_stranz
5th September 2022, 19:01
Resources are compiled and linked into your program at build time. Are these configuration files constant (i.e. the same for every installation of the program)? If so, then you can just add them as resources in the QRC file and then can use them at runtime just like any other disk-based file. Just open them with the proper resource URL (qrc:///...) or file path (:/...) like you would when loading an icon. No need to copy them to a temporary file.

davethomaspilot
7th September 2022, 00:57
Only Qt objects like QFile can use files saved as resources. C calls like Tcl_EvalFile require a name of file that exists--it can't be a Qt resource like :/myfile.tcl

Added after 48 minutes:


Only Qt objects like QFile can use files saved as resources. C calls like Tcl_EvalFile require a name of file that exists--it can't be a Qt resource like :/myfile.tcl

In other words, APIs for library calls won't work with built-in resources. Does that make sense?

For example,

I have several Tcl files that get executed via Tcl_EvalFile(interp, filename) .

"filename" can't be something like ":/myfile.tcl". The API provide by the Tcl C library won't know what to do with a colon prefixed file name.

So, I have to have a copy of the files parked someplace where they can be accessed by file system filename.

d_stranz
7th September 2022, 16:48
Only Qt objects like QFile can use files saved as resources.

Yes, that's correct. QFile (which probably uses QResource internally) recognizes the special syntax of qrc:// or :/ and opens the in-memory resource that is loaded when the program loads.

So use QFile to open your resource and copy it into a file created by QTemporaryFile. See this: QTemporaryFile::createNativeFile(). You should then be able to pass the file name returned by QTemporaryFile::fileName() to your Tcl library. When you delete the QTemporaryFile instance, the file will be deleted as well.