Namespaces
Variants
Actions
Revision as of 09:59, 9 September 2009 by taaidant (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Using resources in Qt

Jump to: navigation, search
Article Metadata

Tested with
Devices(s): N97

Compatibility
Platform(s): Qt

Article
Keywords: Qt, resources
Created: (09 Sep 2009)
Last edited: taaidant (09 Sep 2009)

Contents

Overview

This snippet shows how to add and reference resources in Qt.

Preconditions

Using resources in Qt

QRE.pro

Adding resource file to 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

Adding resources to resource file.

<!-- Copyright (c) 2009 Nokia Corporation  -->
<RCC>
<qresource prefix="/" >
<file>qt-logo.png</file>
</qresource>
</RCC>

qt-logo.png

Qt-logo.png

main.cpp

Referencing 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.

QRE.png

See also

331 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved