Microsoft Visual Studio: Loading resources in Qt application (without plug-in)

六月ゝ 毕业季﹏ 提交于 2019-12-21 17:19:15

问题


We don't have a Qt plug-in installed for MSVS, and it makes me wonder how/whether it is possible to load resources (images, etc) to the application.


回答1:


Yes, you can load ressources. Unfortunately, the qrc Editor which create qrc files is part of the Qt Addin for VS...
But you can create this xml file by hands, for the format see here
Once the qrc file created, you have at least two possibilities :

A) Use qmake

  1. Add a reference to your qrc file in your pro file :

    RESOURCES = ApplicationResources.qrc

  2. Regenerate your vcproj from your pro by using qmake

    qmake -tp vc

B) If you don't generate your vcproj file from your pro file, you can :

  1. Add manually your qrc file in your solution, for example in the following path :

    Resource Files/Res/ApplicationResources.qrc

  2. Add the following commands in the properties of the qrc file in visual studio :
    command line : $(QTDIR)\bin\rcc.exe -name ApplicationResources res\ ApplicationResources.qrc -o $(IntDir)\qrc__ ApplicationResources.cpp
    Description : RCC res/ApplicationResources.qrc
    Output : $(IntDir)\qrc__ ApplicationResources.cpp

C) You can also use an external binary resources file
The command line :rcc -binary myresource.qrc -o myresource.rcc

In the application, you have to register the resource file : QResource::registerResource("/path/to/myresource.rcc");

For using resource file in the source code see the doc

However, like cheez, I also suggest using qmake and pro file and do not edit properties by hand in Visual Studio...

Hope this helps !




回答2:


Use the qrc executable to generate a cpp file which you can include in your project:

/usr/local/Trolltech/Qt-4.5.1/bin/rcc -name core core/core.qrc -o build/release/core/qrc_core.cc

See http://doc.trolltech.com/4.0/resources.html

However, I strongly suggest using qmake or some other build system to automate this for you.



来源:https://stackoverflow.com/questions/1212391/microsoft-visual-studio-loading-resources-in-qt-application-without-plug-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!