[ fromfile: resources.xml id: resources ]
You can use graphic images to add visual impact to an application.
Qt enables projects to make use of binary resources, such as images, sounds, icons, text in some exotic font, and so forth.
Resources such as these are generally stored on disk in separate binary files.
The first step is to list the binary files you want to use in a resource collection file, an XML file that has the extension .qrc
.
<!DOCTYPE RCC> <RCC version="1.0"><qresource> <file alias="images/qh.png">images/qh.png</file> <file alias="images/qd.png">images/qd.png</file> <file alias="images/jc.png">images/jc.png</file> <file alias="images/js.png">images/js.png</file> [...] </qresource> </RCC>
qrc
is an XML file format, used by qtcreator and rcc (Qt's resource compiler), that generally consists of a list of <file>
elements enclosed by <qresource>
tags.
Each <file>
element contains a relative path and an optional file alias.
Add a RESOURCES
line in the project file that contains the name of each qresource file, as shown in Example 9.4.
Example 9.4. src/libs/cards2/cards2.pro
include (../libs.pri) TEMPLATE = lib QT += gui # For locating the files. RESOURCES = cards2.qrc SOURCES += cardpics.cpp \ card.cpp HEADERS += cardpics.h \ card.h \ cards_export.h win32 { DEFINES += CARDS_DLL }
When this project is built, rcc generates an extra file named cards2_qrc.cpp
that contains byte arrays defined in C++.
This file is compiled and linked into the project binary (executable or library) instead of the original card image files.
The DESTDIR
line specifies that the shared object files for libcards2
will be located in $CPPLIBS
with the other libraries that you have built.
Attaching required binary data files to projects as resources makes the project more robust.
The source code does not need to use nonportable pathnames for the resource files.
To refer to a file that is stored as a resource, you can use the alias established in the .rcc file, and precede it with the prefix, :/
.
Each resource then appears (to Qt) to be located in a private virtual file system, rooted at :/
.
These benefits do come with a cost, however. The executable is larger, and the program requires more memory.
Example 9.5 shows some images created with pathnames of this format.
Example 9.5. src/libs/cards2/cardpics.cpp
[ . . . . ] const QString CardPics::values="23456789tjqka"; const QString CardPics::suits="cdhs"; CardPics::CardPics(QObject* parent) : QObject(parent) { foreach (QChar suit, suits) { foreach (QChar value, values) { QString card = QString("%1%2").arg(value).arg(suit); QImage image(fileName(card)); m_images[card]= image; } } } QString CardPics::fileName(QString card) { return QString(":/images/%1.png").arg(card); } QImage CardPics::get(QString card) const { return m_images.value(card.toLower(), QImage()); } [ . . . . ]
There are three Qt classes that facilitate the handling of images.
QImage – designed for off-screen manipulation, input and output operations, and direct pixel access
QPixmap – designed and optimized for drawing on the screen. Used only in the main thread.
QIcon – designed for being cached in video memory and being used frequently, only in the main thread.
QPicture – stores drawing operations rather than the actual bitmap image.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |