简体   繁体   中英

How to use Qt resource files in Visual Studio without a Qt project?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM