2

I want to use Qt's resource system for my program. Visual Studio's resource system is giving me headaches while loading non text based files like .png, .bmp, .jpeg, etc. I have read a bit about Qt and it seems very interesting. I would like to use its resource system. I have downloaded the Qt Visual Studio Tool extension. I have also made a resource file containing the files that I would like to embed in the executable (NOT Qt's executable). I have made a Visual Studio Project, NOT a Qt Project. The only reason I installed the extension is to embed the resources in my executable and access them. Also, if possible I would like to include Qt's headers in my non-Qt Project, so I can easily register and read those resources. So, in a nutshell, this is what I want to do:

  1. Use Qt to embed my resources in the executable.
  2. Access those files in my program.

Something like this:

#include <Qtheaderfiles>

struct FileData {
    unsigned char* bytes;
    unsigned int size;
};

FileData loadFromResource(const std::string& res) {
    QtFile file(res);

    //get all the data from the file and return it in bytes.
    return FileData { fileBytes, fileSize };
}

I expect the above code to compile even when there is no actual file in the directory "res", because all of the resources have been embedded into the executable.

1 Answers1

1

Skimming the Qt documentation on resources:

  1. Create a .qrc file that contains a list of the resources you would like to include
  2. Create a custom build step that invokes rcc on that file (documentation)
  3. Compile and link the resulting cpp source file into your program.
Botje
  • 26,269
  • 3
  • 31
  • 41
  • I have read the documentation, I am still confused. I assume that is only for Qt Projects. How on earth will Visual Studio recognize a .qrc file? – ReluctantProgrammer May 08 '19 at 07:46
  • It will not. That is why you need a custom build step that invokes rcc on the qrc file. In a `Makefile` I would do something like `resources.cpp: resources.cpp\n\trcc -o resources.rcc resources.qrc`. I found [two](https://stackoverflow.com/questions/1212391/microsoft-visual-studio-loading-resources-in-qt-application-without-plug-in) [native VS answers](https://stackoverflow.com/questions/44047364/force-rcc-ing-of-qrc-file-on-each-build) that might help you do it with 'just' VS. – Botje May 08 '19 at 07:51
  • I will try that. Thanks very much :) – ReluctantProgrammer May 08 '19 at 07:55