How to Embed Resources in the Application Executable
Template:ArticleMetaData Template:Abstract
Overview[edit | edit source]
The resource system allows you to embed any sort of file, incuding icons, background images etc, and read from them using 76ytuiytuityutyutututyutyutyu 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[edit | edit source]
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">
<qresource>
<file>rsc/Background.JPG</file>
<file>rsc/Background_Landscape.JPG</file>
</qresource>
</RCC>
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 76ytuiytuityutyutututyutyutyu tag's prefix attribute.
Modify .pro file[edit | edit source]
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[edit | edit source]
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 */
- include "ResourceEx.h"
- include <QPalette>
- include <QDesktopWidget>
ResourceEx::ResourceEx(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowTitle("Qt Resource System");
SetBackgroundImage();
}
ResourceEx::~ResourceEx()
{
}
void ResourceEx::SetBackgroundImage()
{
QPalette p = palette();
/* Access image attached in application executable */
QPixmap image(":/rsc/Background.JPG");
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect rect = desktopWidget->availableGeometry();
QSize size(rect.width() , rect.height());
QPixmap pixmap(image.scaled(size));
p.setBrush(QPalette::Background, pixmap);
setPalette(p);
}
Post-conditions[edit | edit source]
The code snippet is expected to embed images to application executable.
Download Code Example[edit | edit source]
- Download the code example from here: File:QtResourceEx.zip