vicrion
vicrion

Reputation: 1743

How to include qrc data into Qt-based binary that was compiled using CMake

I have a big Qt + third-party-library project which I try to compile into a binary so that to test the program in another machine. I was able to make the binary run (found all the necessary *.dlls and plugins), however, I cannot figure out how to include a *.qrc data with icons for my program. For the moment, the binary cannot load any icons, so I have just buttons with text.

The code structure of my program is as follows:

This is how Data.qrc file looks inside:

RCC>
<qresource prefix="/">
    <file>file-name.svg</file>
    ...

This is how I add the resources to my program, inside the CMakeLists.txt of the program_folder:

qt5_add_resources(IMG_RSC_ADDED data_folder/Data.qrc )
add_executable(${PROJECT_NAME} ${PROJECT_SRCS} ${IMG_RSC_ADDED} )

Inside one of the .cpp files of program_folder I load the icons:

static QIcon icon(QPixmap(":/file-name.svg"));

For the icons, I have a class Data{}; and inside that class I have a set of methods, e.g., static const QIcon& fileIcon();. And in the main code, when I need to use the icon, I call it this way: Data::fileIcon().

It works when I compile and run from the source.

I prepared the binary distribution of my program, and this it how it is structured inside some root folder:

My question is how and where do I put the data files? I tried different locations, e.g., inside the main folder, inside created Data folder. And I simply copied all the data I had (svg file, qrc file), but the binary still cannot see the resources. How to solve it? Or, what is the common practice?

Note, I am using CMake (not QMake) to compile my binary. I am using Qt-5.4, on Windows 7. Let me know if my question lacks details, I will add them. Thanks!

Upvotes: 2

Views: 1045

Answers (1)

ixSci
ixSci

Reputation: 13708

The following command qt5_add_resources(IMG_RSC_ADDED data_folder/Data.qrc) makes source files with binary data generate from each resource from the provied qrc files(Data.qrc in this case) and the list of the resulting files is stored in the IMG_RSC_ADDED variable. Provided this variable is passed to add_executable all the resources get embedded in the resulting binary.

Next, to the actual problem: the problem is not with the distribution but with the code itself. You create and initialize QIcons in the static context and use QPixmap for it but QPixmap requires QApplication up and running which is not the case with the static initialization context. So the proper fix is to move the initialization of the QIcons to some local context(like class ctor or the main function)

Upvotes: 1

Related Questions