Import Qt resources when are main.cpp and GUI classes in dependend static lib project,
Scenario:
I have MSVC2005 with the Qt Visual Studio AddIn installed. In my solution there is a project A consisting of multiple plain C++ code modules, which is built to an executable A.exe
. Project A does not contain a .cpp
file with a main()
, but has a dependency on a project B.
B is a Qt project built as a static lib. It consists on multiple GUI classes an a Qt-typical main.cpp(). Compiling an linking A and B works like a charm and I have a Qt-application. I made some forks of A (Afork1, Afork2, etc.) that all share the same interface to the GUI but have different customizations of the underlying business logic. I can batch build A.exe
, Afork1.exe
, ..., etc. and they all wi开发者_StackOverflow中文版ll have the same GUI, which is pretty neat.
Problem:
In B there is a .qrc
file with multiple images, icons, etc. that are used in the GUI. The GUI classes are crafted with Qt Designer and I added the ressources to the GUI widgets. After building and running A.exe
I cannot see these icons and images, the space for them is reserved in the widgets but not filled with the content. I guess, the ressources are not linked into the executable. Is there a way to ensure the linking of ressources that are part of Qt static lib project?
You need to initialize the resources explicitly. From the Qt docs:
If you have resources in a static library, you might need to force initialization of your resources by calling Q_INIT_RESOURCE() with the base name of the .qrc file. For example:
int main(int argc, char *argv[]) { QApplication app(argc, argv); Q_INIT_RESOURCE(graphlib); ... return app.exec(); }
精彩评论