2009/05/12

QT Resource File

Create your own RESOURCE FILE with .qrc extension, and its format as below
<!DOCTYPE RCC><RCC version="1.0"> 
<qresource>
    <file>Panda_32x32.png</file>     
<file>Turtle_32x32.png</file> 
</qresource> 
</RCC>
Execute qmake -project, the RESOURCES += myRC.qrc will be added in project (.pro) file

##################################################################### 
# Automatically generated by qmake (2.01a) ?? 5? 12 11:36:22 2009 #
##################################################################### 
TEMPLATE = app 
TARGET =  
DEPENDPATH += . 
INCLUDEPATH += .  
#
Input SOURCES += qrc.cpp 
RESOURCES += myRC.qrc

qmake again to generate Makefile in Makefile...
 
qrc_myRC.cpp: myRC.qrc \
                Turtle_32x32.png \
                Panda_32x32.png
        /opt/qtsdk-2009.01/qt/bin/rcc -name myRC myRC.qrc -o qrc_myRC.cpp
The qrc_myRC.cpp will be generated if make is executed. Now we can execute make to produce the executable file check the contents of qrc_myRC.cpp after make......
 
static const unsigned char qt_resource_data[] = {
  // /home/chanyenping/200905/IMAGE/_QtRC/Turtle_32x32.png
  0x0,0x0,0x9,0x40,
  0x89,
  0x50,0x4e,0x47,0xd,0xa,0x1a,0xa,0x0,0x0,0x0,0xd,0x49,0x48,0x44,0x52,0x0,
  0x0,0x0,0x20,0x0,0x0,0x0,0x20,0x8,0x6,0x0,0x0,0x0,0x73,0x7a,0x7a,0xf4,
  0x0,0x0,0x9,0x7,0x49,0x44,0x41,0x54,0x58,0x85,0xa5,0xd7,0x79,0x6c,0x1c,0xe5,
  0x19,0xc7,0xf1,0xef,0x9c,0xbb,0x3b,0xbb,0x6b,0x8f,0x9d,0xb5,0xe3,0x38,0x8e,0xe3,
  0x90,0x90,0x84,0x46,0x1,0x87,0x82,0x10,0x24,0x28,0x49,0x21,0x10,0x28,0x12,0xa6
:
:
Execute the application, and get the screenshot as below. (Icons and application in the same directory.)

QListWidget *listWidget = new QListWidget;
listWidget->insertItem(0, new QListWidgetItem(
           QIcon(":/Butterfly_32x32.png"), "Butterfly"));
listWidget->insertItem(1, new QListWidgetItem(
           QIcon("Dolphin_32x32.png"), "Dolphin"));
listWidget->insertItem(2, new QListWidgetItem(
           QIcon("Elephant_32x32.png"),"Elephant"));


Please be noted that if embedded icons to be used, you shall add :/ before your resource file. Otherwise no icons will appear in your listWidget. When application and icons are in different directory, you will see only the butterfly icon in the listWidget as below. (Icons and application in different directory) Check the file size, the _QtRC (executable with embedded resources) has larger size than the executable _QtRCwoRC without embedded resources.
-rwxr-xr-x 1 cyp1973 cyp1973 32455 2009-05-12 12:05 _QtRC
-rwxr-xr-x 1 cyp1973 cyp1973 14565 2009-05-12 11:00 _QtRCwoRC

ICONS SOURCE, http://www.visualpharm.com/animals_icon_set/ .

沒有留言: