Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.
How to Embed Resources in the Application Executable
This code example shows how to embed binary files in the application's executable using the Qt Resource System.
The resource system allows you to embed any sort of file, incuding icons, background images etc, and read from them using QFile in almost the same manner as you would any other file. Note that including binaries in the executable means that they can't be replaced separately, and means that on devices that do not have on demand code paging, more memory is required.
Create .qrc(Resource Collection) file
Create .qrc file (in XML-based file format) and define the resources that are part of the application's source tree. For example.
/* ResourceEx.qrc */
<!DOCTYPE RCC><RCC version="1.0">
The name of file can be changed using the file tag's alias attribute. You can also specify a path prefix for all files in the .qrc file using the qresource tag's prefix attribute.
Modify .pro file
Assign .qrc file to RESOURCES variable so that qmake knows about it and will produce make rules to generate a file called .cpp (in this example qrc_ResourceEx.cpp) that is linked into the application.
RESOURCES += ResourceEx.qrc
Access resources from the application
The colon (:) prefix informs the Qt-handling methods that the file needs to be fetched from an application resource in the executable rather than an external file. Because it is not an external file, we don’t need to worry about where the file is located. Resources are accessible in the application with a :/ prefix. For example, the path :/rsc/Background.JPG would give access to the Background.JPG file, whose location in the application's source tree is /rsc/Background.JPG.
/* ResourceEx.cpp */
setWindowTitle("Qt Resource System");
QPalette p = palette();
/* Access image attached in application executable */
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect rect = desktopWidget->availableGeometry();
QSize size(rect.width() , rect.height());
The code snippet is expected to embed images to application executable.
Download Code Example
- Download the code example from here: File:QtResourceEx.zip