Using resources in Qt
hamishwillee
(Talk | contribs) m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Fix ArticleMetaData) |
||
| Line 1: | Line 1: | ||
| + | [[Category:Qt]][[Category:Code Examples]][[Category:Code Snippet]] | ||
{{KBCS}} | {{KBCS}} | ||
{{ArticleMetaData | {{ArticleMetaData | ||
| Line 6: | Line 7: | ||
|category=Qt | |category=Qt | ||
|subcategory=Files/Data | |subcategory=Files/Data | ||
| − | |creationdate= | + | |creationdate=20091124 |
|keywords=Qt, resources | |keywords=Qt, resources | ||
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) --> | |sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) --> | ||
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
| − | |sdk= | + | |sdk=Qt 4.5 Garden Pre-release |
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) --> | |devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) --> | ||
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer --> | |signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer --> | ||
| Line 24: | Line 25: | ||
==Preconditions== | ==Preconditions== | ||
| − | + | None | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
== Using resources in Qt == | == Using resources in Qt == | ||
| Line 38: | Line 33: | ||
Add a resource file to a project file: | Add a resource file to a project file: | ||
| − | <code> | + | <code text> |
# Copyright (c) 2009 Nokia Corporation | # Copyright (c) 2009 Nokia Corporation | ||
TEMPLATE = app | TEMPLATE = app | ||
| Line 53: | Line 48: | ||
Add resources to a resource file: | Add resources to a resource file: | ||
| − | <code> | + | <code text> |
<!-- Copyright (c) 2009 Nokia Corporation --> | <!-- Copyright (c) 2009 Nokia Corporation --> | ||
<RCC> | <RCC> | ||
| Line 117: | Line 112: | ||
* [http://doc.trolltech.com/4.7/resources.html The Qt Resource System] | * [http://doc.trolltech.com/4.7/resources.html The Qt Resource System] | ||
| − | |||
| − | |||
Revision as of 07:02, 27 October 2011
Article Metadata
Tested with
SDK: Qt 4.5 Garden Pre-release
Devices(s): Nokia N97
Compatibility
Platform(s): Qt
Article
Keywords: Qt, resources
Created: taaidant
(24 Nov 2009)
Last edited: hamishwillee
(27 Oct 2011)
Contents |
Overview
This snippet shows how to add and reference resources in Qt.
Preconditions
None
Using resources in Qt
QRE.pro
Add a resource file to a project file:
# Copyright (c) 2009 Nokia Corporation
TEMPLATE = app
TARGET = QRE
QT += core \
gui
SOURCES += main.cpp
# This is how the qre.qrc is added to the project.
RESOURCES += qre.qrc
qre.qrc
Add resources to a resource file:
<!-- Copyright (c) 2009 Nokia Corporation -->
<RCC>
<qresource prefix="/" >
<file>qt-logo.png</file>
</qresource>
</RCC>
qt-logo.png
main.cpp
Reference resources from the code:
/*
* Copyright (c) 2009 Nokia Corporation
*/
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
/* QMainWindow as our window */
QMainWindow* w = new QMainWindow;
/* QFrame as our centralWidget */
QFrame* centralW = new QFrame(w);
w->setCentralWidget(centralW);
/* QVBoxLayout as our central widgets layout */
QVBoxLayout* layout = new QVBoxLayout(centralW);
centralW->setLayout(layout);
/* QLabel is used as our logo container. */
QLabel* qtLogoContainer = new QLabel;
/* QPixmap is used to load the image from resources
* defined in qre.qrc file. */
qtLogoContainer->setPixmap(QPixmap(":/qt-logo.png"));
/* To get the logo shown we need to add the container containing
* the logo to the layout. */
layout->addWidget(qtLogoContainer);
/* We'll display the window maximized. */
w->showMaximized();
/* Run till the execution ends. */
int returnValue = a.exec();
/* Flag the window to be deleted.*/
w->deleteLater();
/* Return the execution return value.*/
return returnValue;
}
Postconditions
Now you know how to add and reference resources in Qt.


