使用Qt Resource System将resource文件打包进二进制程序

来源:互联网 发布:杨百万软件多少钱 编辑:程序博客网 时间:2024/05/01 05:18

为什么需要Qt Resource System? 它能帮助我们将所有的resource 文件(图片,qml,等等)都装进我们的程序中。

要做到这点,你需要完成下面的一些步骤:

1. 创建qrc文件,并在pro文件中配置

我有一个resources.qrc文件,在我的gui.pro文件中,我像这样配置

QT += qml quickTARGET = gui!android: !ios: !blackberry: qtHaveModule(widgets): QT += widgetsinclude(src/src.pri)RESOURCES += \    resources.qrcHEADERS +=

2. 编辑resources.qrc文件,包含project目录下的所有resource 文件。

在我的proejct目录下,我有一些qml文件位于qmls目录和它的子目录,resources.qrc文件在project目录下。一些image文件在images目录下。看我下面的目录树:

├── gui.pro├── images│   ├── header.png│   └── selectedrow.png├── qmls│   ├── a.qml│   ├── main.qml│   ├── menu.qml│   ├── props│   │   ├── input│   │   │   └── b.qml│   │   ├── output│   │   └── processor│   ├── README├── resources.qrc└── src    ├── main.cpp    └── src.pri
将所有这些resource文件添加到resources.qrc中。

<!DOCTYPE RCC><RCC version="1.0">  <qresource prefix="/">    <file>qmls/main.qml</file>    <file>qmls/menu.qml</file>    <file>qmls/a.qml</file>    <file>qmls/props/input/b.qml</file>    <file>images/selectedrow.png</file>    <file>images/header.png</file>  </qresource></RCC>

所有上面配置的resource文件都会被编译到app的可执行程序中。


3. 在main.cpp中,使用QUrl类加载main.qml文件

QQmlApplicationEngine engine(QUrl("qrc:/qmls/main.qml"));

4. 在qml文件中,使用相对路径去加载其他的resource文件, 比如:

Button {        id: saveMenu        anchors.left: openMenu.right        text: "Save"        iconSource: "../images/selectedrow.png"

这就是全部了。当编译你的程序时,Qt会创建qrc_resources.cpp文件,里面包含了静态C++数组,数组中包含了被压缩的来自resource文件的数据。下面的代码片段来自我的project的qrc_resources.cpp文件。

#include <QtCore/qglobal.h>static const unsigned char qt_resource_data[] = {  // /home/dean/work/arcgis-3d-scene-builder/source/builder/gui/qmls/main.qml  0x0,0x0,0x2,0x38,  0x0,  0x0,0xa,0x71,0x78,0x9c,0xdd,0x55,0xdf,0x8f,0x9a,0x40,0x10,0x7e,0xe7,0xaf,0x98,  0xf8,0xd8,0x26,0x4,0xb1,0xb5,0x17,0x12,0xd3,0xf4,0x4c,0xd3,0x36,0x69,0x93,0xb3,  0x5e,0xda,0xbe,0xae,0xcb,0xa8,0x9b,0x2e,0x2c,0xdd,0x1d,0xe2,0x99,0x8b,0xff,0x7b,  0x17,0x4,0x14,0x1,0x83,0xda,0x7b,0xe9,0x3e,0x0,0x3b,0xf3,0xed,0xfc,0xf8,0x76,  0x66,0x10,0x51,0xa2,0x34,0xc1,0x8c,0x66,0xa9,0xe0,0xbf,0xc1,0x77,0x87,0x8e,0xa8,...




0 0
原创粉丝点击