×
Namespaces

Variants
Actions
Revision as of 04:17, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to Embed Resources in the Application Executable

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleCompatibility
Platform(s):
Symbian
Article
Created: Yashu (29 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)

This code example shows how to embed binary files in the application's executable using the Qt Resource System.

Contents

Overview

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">
<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 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 */
#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

The code snippet is expected to embed images to application executable.

Download Code Example

This page was last modified on 11 October 2012, at 04:17.
119 page views in the last 30 days.
×