QT-qrc资源管理

QT中对图片、qml资源都是通过qrc来管理的,本质上就是将这些资源通过二进制的方式,放入一个静态数据中,在编译时一块编译到二进制中。这块有两个疑惑,

  1. 通过qrc加载对应文件时怎么找到对应的二进制数组 
  2. 不同模块中的二进制资源如果相互引用的问题

第一个问题,没有看过源码,但是大致猜测,应该是QT框架维护了一套qrc路径与二进制数组的映射关系,可以在使用qrc资源的地方替换为实际的资源二进制数据

第二个是因为QT在编译qrc时不仅生成了二进制静态数组,还生成了初始化资源的接口,同时有一个全局变量,在全局变量构造时会调用这个接口,这个接口的作用就是将自身模块中的资源

信息注册到qrc资源管理系统中,这样在整个进程中都可以通过访问这个qrc资源管理系统拿到对应的二进制资源。

有一点需要注意,windows上在连接资源dll的时候,一定要实际调用dll中至少一个接口,否则编译器会认为不需要该dll,不去链接。也就导致了dll实际不被加载,资源自然也不会初始化注册了

附上qrc生成的cpp资源文件:

/****************************************************************************
** Resource object code
**
** Created by: The Resource Compiler for Qt version 5.14.2
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/static const unsigned char qt_resource_data[] = {// D:/OneDrive/Code/QT/dll/main_rect.qml0x0,0x0,0x1,0xc1,0xef,0xbb,0xbf,0x69,0x6d,0x70,0x6f,0x72,0x74,0x20,0x51,0x74,0x51,0x75,0x69,0x63,0x6b,0x20,0x32,0x2e,0x31,0x34,0xd,0xa,0x69,0x6d,0x70,0x6f,0x72,0x74,0x20,0x51,0x74,0x51,0x75,0x69,0x63,0x6b,0x2e,0x57,0x69,0x6e,0x64,0x6f,0x77,0x20,0x32,0x2e,0x31,0x34,0xd,0xa,0x69,0x6d,0x70,0x6f,0x72,0x74,0x20,0x51,0x74,0x51,0x75,0x69,0x63,0x6b,0x2e,0x43,0x6f,0x6e,0x74,0x72,0x6f,0x6c,0x73,0x20,0x31,0x2e,0x34,0xd,0xa,0x69,0x6d,0x70,0x6f,0x72,0x74,0x20,0x51,0x74,0x51,0x75,0x69,0x63,0x6b,0x2e,0x43,0x6f,0x6e,0x74,0x72,0x6f,0x6c,0x73,0x2e,0x53,0x74,0x79,0x6c,0x65,0x73,0x20,0x31,0x2e,0x34,0xd,0xa,0x69,0x6d,0x70,0x6f,0x72,0x74,0x20,0x51,0x74,0x47,0x72,0x61,0x70,0x68,0x69,0x63,0x61,0x6c,0x45,0x66,0x66,0x65,0x63,0x74,0x73,0x20,0x31,0x2e,0x31,0x32,0xd,0xa,0xd,0xa,0x41,0x70,0x70,0x6c,0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x57,0x69,0x6e,0x64,0x6f,0x77,0x20,0x7b,0xd,0xa,0x20,0x20,0x20,0x20,0x76,0x69,0x73,0x69,0x62,0x6c,0x65,0x3a,0x20,0x74,0x72,0x75,0x65,0xd,0xa,0x20,0x20,0x20,0x20,0x77,0x69,0x64,0x74,0x68,0x3a,0x20,0x36,0x34,0x30,0xd,0xa,0x20,0x20,0x20,0x20,0x68,0x65,0x69,0x67,0x68,0x74,0x3a,0x20,0x34,0x38,0x30,0xd,0xa,0x20,0x20,0x20,0x20,0x74,0x69,0x74,0x6c,0x65,0x3a,0x20,0x71,0x73,0x54,0x72,0x28,0x22,0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x22,0x29,0xd,0xa,0x20,0x20,0x20,0x20,0x52,0x65,0x63,0x74,0x61,0x6e,0x67,0x6c,0x65,0x7b,0xd,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x69,0x64,0x3a,0x20,0x72,0x6f,0x6f,0x74,0xd,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x61,0x6e,0x63,0x68,0x6f,0x72,0x73,0x2e,0x66,0x69,0x6c,0x6c,0x3a,0x20,0x70,0x61,0x72,0x65,0x6e,0x74,0xd,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x49,0x6d,0x61,0x67,0x65,0x20,0x7b,0xd,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x69,0x64,0x3a,0x20,0x69,0x69,0x69,0xd,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x73,0x6f,0x75,0x72,0x63,0x65,0x3a,0x20,0x22,0x71,0x72,0x63,0x3a,0x2f,0x31,0x32,0x33,0x2e,0x6a,0x70,0x67,0x22,0xd,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x61,0x6e,0x63,0x68,0x6f,0x72,0x73,0x2e,0x66,0x69,0x6c,0x6c,0x3a,0x20,0x70,0x61,0x72,0x65,0x6e,0x74,0xd,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7d,0xd,0xa,0x20,0x20,0x20,0x20,0x7d,0xd,0xa,0x7d,0xd,0xa,};static const unsigned char qt_resource_name[] = {// main_rect.qml0x0,0xd,0xc,0xa7,0xdc,0xfc,0x0,0x6d,0x0,0x61,0x0,0x69,0x0,0x6e,0x0,0x5f,0x0,0x72,0x0,0x65,0x0,0x63,0x0,0x74,0x0,0x2e,0x0,0x71,0x0,0x6d,0x0,0x6c,};static const unsigned char qt_resource_struct[] = {// :0x0,0x0,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x1,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,// :/main_rect.qml0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x0,
0x0,0x0,0x1,0x79,0x9d,0x1b,0xc6,0x5a,};#ifdef QT_NAMESPACE
#  define QT_RCC_PREPEND_NAMESPACE(name) ::QT_NAMESPACE::name
#  define QT_RCC_MANGLE_NAMESPACE0(x) x
#  define QT_RCC_MANGLE_NAMESPACE1(a, b) a##_##b
#  define QT_RCC_MANGLE_NAMESPACE2(a, b) QT_RCC_MANGLE_NAMESPACE1(a,b)
#  define QT_RCC_MANGLE_NAMESPACE(name) QT_RCC_MANGLE_NAMESPACE2( QT_RCC_MANGLE_NAMESPACE0(name), QT_RCC_MANGLE_NAMESPACE0(QT_NAMESPACE))
#else
#   define QT_RCC_PREPEND_NAMESPACE(name) name
#   define QT_RCC_MANGLE_NAMESPACE(name) name
#endif#ifdef QT_NAMESPACE
namespace QT_NAMESPACE {
#endifbool qRegisterResourceData(int, const unsigned char *, const unsigned char *, const unsigned char *);
bool qUnregisterResourceData(int, const unsigned char *, const unsigned char *, const unsigned char *);#ifdef QT_NAMESPACE
}
#endifint QT_RCC_MANGLE_NAMESPACE(qInitResources_resource)();
int QT_RCC_MANGLE_NAMESPACE(qInitResources_resource)()
{int version = 3;QT_RCC_PREPEND_NAMESPACE(qRegisterResourceData)(version, qt_resource_struct, qt_resource_name, qt_resource_data);return 1;
}int QT_RCC_MANGLE_NAMESPACE(qCleanupResources_resource)();
int QT_RCC_MANGLE_NAMESPACE(qCleanupResources_resource)()
{int version = 3;QT_RCC_PREPEND_NAMESPACE(qUnregisterResourceData)(version, qt_resource_struct, qt_resource_name, qt_resource_data);return 1;
}namespace {struct initializer {initializer() { QT_RCC_MANGLE_NAMESPACE(qInitResources_resource)(); }~initializer() { QT_RCC_MANGLE_NAMESPACE(qCleanupResources_resource)(); }} dummy;
}

 

Published by

风君子

独自遨游何稽首 揭天掀地慰生平