Qt and standard resource system
-
This is my first post, so hello everyone.
I would like to know is it possible to use standard resources with Qt.
I have build my Qt project with MSVC so I cant use Qt resources (I suppose). Is it possible to extract resources and use with for example QIcon?Example:
resource.h#define IDB_ICO_MAIN 110 #define IDB_ICO_CONFIG 111
resource.rc
IDB_ICO_MAIN RCDATA "imain.png" IDB_ICO_CONFIG RCDATA "iconfig.png"
window.cpp
(...) QIcon icon_main = new QIcon( [IDB_ICO_MAIN] ); QIcon icon_config = new QIcon( [IDB_ICO_CONFIG] );
Something like that.
-
Hi, welcome to Devnet.
You can use qrc files, even if you build your project with MSVC. The file edition might be more painful, but still feasible. Did you try it, or simply assumed that it was not possible?
And what about using QtCreator instead? It's really well designed, with tons of shortcuts and features. -
I read this article but didnt find explanation how to use resources without QtCreator so I assumed that it is impossible.
Yes I have tried QtCreator it's nice, but I preffer Visual Studio.So in steps:
1. Create .qrc file<!DOCTYPE RCC> <RCC version="1.0"> <qresource> <file>images/icon.png</file> </qresource> </RCC>
2. Compile .qrc file
rcc -binary resource.qrc -o resource.rcc
3. Include compiled .rcc file into executable
IDB_RES RCDATA "resources.rcc"
4. Init resources in main()
Q_INIT_RESOURCE(IDB_RES);
Do I understand it well?
-
Hi
Do you use the Qt plugin for Visual Studio ?As far as I know it handles the resources for you.
you could try the
Diagram Scene Example
as it has icons for toolbar and see if it just work.
update:
http://doc.qt.io/vs-addin/ -
There are two ways to use Qt resources.
-
As an external binary that you load at runtime:
- create the
resource.qrc
file - compile into a binary:
rcc -binary C:/path/to/resource.qrc -o C:/path/to/resource.rcc
- load at runtime:
QResource::registerResource("C:/path/to/resource.rcc");
- use
- create the
-
As an internal resource compiled into the executable:
- create the
resource.qrc
file - generate c++ file:
rcc -name "someName" -no-compress C:/path/to/resource.qrc -o C:/path/to/resource.cpp
- include the generated
resource.cpp
in your solution - use
- create the
As @mrjj pointed out the Visual Studio Add-In uses the second method and does the generation and including for you, so all you have to do when you use it is create the qrc file, include it in your solution and that's it.
-
-
When just adding the images.qrc to resources in pro file, Qt generates a .cpp file when compiling the application out of the images that are defined in the .qrc file. The compile output is as follows:
/usr/local/Qt-5.5.1/bin/rcc -name images ../myApplication/images.qrc -o qrc_images.cpp
g++ -c -pipe -g -std=c++0x -Wall -W -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_QUICK_LIB -DQT_MULTIMEDIA_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_SQL_LIB -DQT_CORE_LIB -I../myApplication -I. -I../shared_base/Debug -I../shared_base -I/usr/local/Qt-5.5.1/include -I/usr/local/Qt-5.5.1/include/QtQuick -I/usr/local/Qt-5.5.1/include/QtMultimedia -I/usr/local/Qt-5.5.1/include/QtGui -I/usr/local/Qt-5.5.1/include/QtQml -I/usr/local/Qt-5.5.1/include/QtNetwork -I/usr/local/Qt-5.5.1/include/QtSql -I/usr/local/Qt-5.5.1/include/QtCore -I. -I/usr/local/Qt-5.5.1/mkspecs/linux-g++ -o qrc_images.o qrc_images.cppSo as seen in the output, to compile the image resources, two different commands are executed, the rcc and the g++. However, as you mentioned, one can simply compile the images with the rcc and register this binary file in the application during run time. I can't understand what this g++ command does and why the cpp step is necessary.
Also why does qt include libs such as Multimedia, Gui, etc. into this file and make it larger than just the images?
Note: The images folder is sized 27MB. The generated images.cpp file is sized 66MB and if I compile the images with the rcc-utility myself it is also 27MB and it works just like the 66MB did.
-
@onurA This is discussed here: http://forum.qt.io/topic/64734/qt-resource-system/2
-
@Chris-Kawa Hi Could you pls help me understanding the last point "Use".... I have project which has four .qml files... i have created rcc from qrc...i have loaded .rcc instead of loadin qml...but still i dont know how to use it to display the window which is present in the qml.