VS2012中QT资源文件qrc加载失败(Q_INIT_RESOURCE报错)

在QT中如果需要使用资源时,会用到一个qrc文件.qrc文件本质上是一个XML文件,里面描述了相应资源的位置。一般将需要引用的资源放到与main.cpp同级的目录。我用VS2012开发QT,如果按正常的情况来建立QT,会带有一个qrc文件和对就的qrc*.cpp的文件,如下图(qrcexample.qrc和qrc_qrcexample.cpp)。

VS2012中QT资源文件qrc加载失败(Q_INIT_RESOURCE报错)_第1张图片

其中qrc_qrcexample.cpp是自动生成的,是在qrc名字的前面加了一个qrc_的前缀。如果这样正常添加资源文件是没有问题的,但是我却误删了qrc_qrcexample.cpp文件。等到想用qrc来加载图片时,按着正常的操作,在qrcexample.qrc中加了相应的图片,可是编译之后,怎么引用路径都无法得到图片。这到底是怎么了?找了很多资料,说是可以用Q_INT_RESOURCE在main函数中强制加载qrc文件,可是却报错了。(附:Q_INT_RESOURCE的调用方式是,Q_INT_RESOURCE(qrc文件名),注意一定要是qrc文件名。)

VS2012中QT资源文件qrc加载失败(Q_INIT_RESOURCE报错)_第2张图片

一般来说,无法解析外部符号,是因为函数有定义却没有去实现。那这里的qInitResources_qrcexample(void)根本就没有定义过,又怎么可能去实现呢?那只有一种解释,该函数是由系统自动生成并实现了。现在系统生成了该函数的定义,却没有相应的实现。那怎么办?

于是网上查找资料,后面找到了一篇文章,说是系统会生成一个与qrc资源文件相对应的CPP(也就是前面误删的qrc_qrcexample.cpp)。既然误删了,那是不是可以再建一个相同的?结果证明,这种想法太天真了。因为只是建一个这样的文件,里面什么内容都没有,那不是形同虚设吗?

想着,这个文件既然是系统生成的,那必然会在系统中有所配置,可是在找遍了配置项后,都没有找到一个对应的。后面就想着,VS的工程文件中有对引用和编译信息的配置,会不是存在那里呢?如果存在了那里,那我又要如何才能分辨出哪个是呢?最后的办法就是找一个正常的来对比。立即新建一个项目test,自动生成了test.qrc和qrc_test.cpp,用UE打开test.vcxproj,是一个XML格式的文件,搜索qrc_test.cpp,果然有相应的配置信息。

 
      
      
      
      
    
依样画葫芦,用UE打开QrcExample.vcxproj,然后找到相应的位置,添加上相应的节点内容,下图的红框部分就是添加的。

VS2012中QT资源文件qrc加载失败(Q_INIT_RESOURCE报错)_第3张图片

然后重新加载项目,这时系统自动生成了qrc_qrcexample.cpp,其内容如下。

/****************************************************************************
** Resource object code
**
** Created by: The Resource Compiler for Qt version 5.1.1
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

#include 

QT_BEGIN_NAMESPACE

QT_END_NAMESPACE


int QT_MANGLE_NAMESPACE(qInitResources_qrcexample)()
{
    return 1;
}

Q_CONSTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qInitResources_qrcexample))

int QT_MANGLE_NAMESPACE(qCleanupResources_qrcexample)()
{
    return 1;
}

Q_DESTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qCleanupResources_qrcexample))

这时再编译,已经OK了。再回过头来看一下,会看到前面报错的qInitResources_qrcexample,这个文件中已经有相应的实现了。不过,仔细的朋友会发现,这个文件已经不是在GeneratedFiles的文件夹下,而是在SourceFiles文件夹了。那会不会有影响呢?我们可以添加一些资源进去,编译后资源会被写到qrc_qrcexapmle.cpp中,那一串串的十六进制就是图片的数据。测试结果证明是OK的。

/****************************************************************************
** Resource object code
**
** Created by: The Resource Compiler for Qt version 5.1.1
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

#include 

static const unsigned char qt_resource_data[] = {
  // G:/Code/QT/QrcExample/QrcExample/images/about.png
  0x0,0x0,0x4,0xad,
  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,0x4,0x74,0x49,0x44,0x41,0x54,0x58,0x85,0xd5,0x96,0xcd,0x6f,0x1b,0x45,
  0x18,0xc6,0x9f,0x9d,0x5d,0xaf,0xbd,0xb6,0x93,0x6e,0x5d,0xb7,0x84,0x34,0x6a,0xd2,
  0xb4,0x4d,0x8a,0xd4,0x95,0x82,0x44,0x11,0x42,0x95,0xaa,0x52,0x50,0x85,0x8a,0x44,
  0x55,0xe,0x80,0xc4,0xa1,0x2a,0xfc,0x1,0xec,0x1,0xe,0x70,0xe8,0x21,0x12,0xb7,
  0xca,0x27,0x24,0x40,0xa2,0x42,0xa8,0x2,0x71,0xea,0x1,0x89,0xb,0x8,0x4a,0x85,
  0x42,0x94,0xa6,0xa1,0xad,0xab,0x7c,0xb8,0x51,0xbe,0x1c,0xa7,0x76,0xba,0x4d,0xd7,
  0xde,0x5d,0x7b,0xbf,0x66,0x87,0x83,0x71,0x71,0xea,0x38,0x71,0x20,0x29,0xe2,0xb9,
  0xed,0xbb,0x33,0xf3,0xfc,0xe6,0xeb,0x7d,0x7,0xf8,0x8f,0xc5,0x6d,0xaa,0x75,0x2a,
  0x2d,0x2,0x38,0x2,0xe0,0x60,0x9b,0xc8,0x77,0x46,0x42,0x24,0xe,0x0,0xb6,0x17,
  0x98,0x86,0x4b,0xf3,0x0,0x32,0x0,0xee,0x40,0x55,0xdc,0xad,0x3,0x48,0xa5,0x13,
  0x31,0x91,0xbc,0x75,0x70,0x97,0xf4,0x46,0x42,0x12,0x5e,0x4c,0x46,0x43,0x91,0x84,
  0x24,0x40,0xe,0xf3,0x88,0xf0,0xd5,0xee,0x36,0x65,0xd0,0x1d,0x8a,0x15,0xdb,0x87,
  0x66,0x79,0xf6,0xc3,0x8a,0x3f,0x34,0xfd,0xc0,0xbe,0x62,0xba,0xf4,0x1b,0xa8,0xca,
  0xca,0x3f,0x3,0x48,0xa5,0x3b,0xba,0xda,0xc5,0x8f,0xfb,0x77,0x47,0xcf,0x1f,0x4e,
  0x4a,0xd1,0x23,0x3b,0x45,0xb4,0x8b,0x4,0x22,0x1,0x78,0x8e,0x43,0x85,0x32,0x94,
  0xc,0x1b,0x1c,0xc7,0xa1,0x2d,0x1e,0x86,0xc4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,
  0x56,0x49,0x20,0x9c,0x40,0x2c,0x37,0x58,0x76,0x69,0x93,0xb,0xbf,0x8d,0xf2,0x29,
  0x83,0xe9,0x50,0x8d,0x0,0x18,0x2d,0x98,0xde,0x13,0x7,0x28,0x58,0x2e,0x18,0x30,
  0x4a,0x0,0x8c,0x67,0x8b,0xce,0xf8,0x93,0x6,0x58,0x2c,0x3a,0x19,0x0,0xb7,0x9,
  0x54,0x5,0x13,0xcb,0xd6,0xc5,0xd2,0x63,0xd7,0x6a,0x3b,0x65,0xba,0x14,0xe3,0xcb,
  0xe5,0x8b,0x50,0x95,0x6a,0x2a,0xb6,0x7d,0xf6,0xd5,0x48,0xd6,0x18,0x62,0x6c,0xe3,
  0x74,0xfa,0x6f,0xc5,0x18,0xc3,0x48,0xd6,0x18,0x29,0x7b,0xc1,0x25,0xa0,0x56,0xb,
  0x54,0x25,0x98,0xd2,0x2a,0x6f,0x8f,0x2d,0x99,0x6b,0xd6,0xec,0xad,0xd4,0xcd,0x7b,
  0x56,0x7e,0xe2,0x7e,0xf9,0x4d,0xa8,0x8a,0xff,0x37,0x40,0x15,0x62,0x61,0x38,0x6b,
  0x9c,0xbc,0x91,0x33,0x96,0xb6,0x63,0x25,0x18,0x63,0x18,0x5b,0x32,0xf2,0x43,0xb,
  0xa5,0x53,0x50,0x95,0xb9,0x5a,0x7c,0x75,0x6,0x52,0x95,0xf1,0xe1,0xac,0x71,0xf4,
  0xe7,0x19,0xfd,0x9a,0xe5,0xd2,0x2d,0x33,0xb7,0x5c,0x8a,0x5f,0x66,0xf4,0xa1,0xdf,
  0x17,0x8c,0xa3,0x50,0x95,0xdb,0xf5,0xff,0x1a,0x53,0xb0,0xaa,0x2c,0x4d,0xa6,0xd2,
  0x27,0xb2,0x45,0xe7,0xa3,0x33,0xcf,0x24,0x7,0x6b,0x6f,0x83,0x7a,0x39,0x1e,0xc5,
  0xcd,0x59,0x7d,0x55,0x8c,0x6,0x8d,0xab,0xe6,0xf8,0x1,0x26,0xef,0x97,0xb5,0xdb,
  0x79,0xeb,0x42,0xc9,0xa1,0x9f,0x41,0x55,0x1a,0x12,0xce,0x7a,0x6f,0xc2,0x33,0xea,
  0xb,0x4f,0x5f,0x39,0xbc,0x23,0x84,0x82,0x1d,0x60,0xd9,0x9,0x50,0xb3,0x58,0xaf,
  0x18,0x79,0x34,0xc0,0x52,0xc9,0xc5,0x3d,0xc3,0x1d,0x9b,0xd2,0xca,0x5f,0x9a,0x6e,
  0xf0,0x35,0x54,0xc5,0x6c,0x66,0xd3,0xb4,0x8,0xc5,0x45,0xd2,0xd7,0x2e,0x12,0x10,
  0xe,0xe0,0x38,0x80,0x1,0xc8,0x68,0x65,0xf3,0xae,0x56,0xb9,0x2c,0x4b,0x42,0x4f,
  0x44,0x20,0x9d,0x84,0xe3,0xe2,0x1c,0x0,0xca,0x98,0xe9,0x94,0x2a,0x79,0xdd,0x36,
  0x32,0x8b,0x45,0xf7,0xba,0x17,0xb0,0xab,0x50,0x95,0x85,0xa6,0x93,0x6b,0x5,0x60,
  0x4f,0x5c,0xec,0x7,0x7,0xcc,0x5b,0x14,0xf,0x3d,0x86,0x89,0xe5,0xb2,0x7e,0x75,
  0x56,0x3f,0x1d,0xbc,0xaf,0xc,0xb5,0x32,0x70,0xab,0x6a,0xa,0xb0,0x23,0xcc,0xf7,
  0x15,0x5d,0x6,0x8f,0x1,0x77,0xa,0x96,0x76,0x6d,0xb6,0xf8,0x2a,0x53,0x95,0xd1,
  0xad,0x34,0x5f,0x17,0x40,0xa,0xf1,0x7d,0x1e,0x3,0x6e,0xdd,0x33,0x97,0x7f,0x9b,
  0x2f,0xbd,0xf2,0xf8,0xe9,0xdd,0x5e,0x80,0x54,0x5a,0x8e,0x89,0x64,0xcf,0x8d,0x9c,
  0xb1,0x34,0x9c,0x35,0x4e,0x42,0x55,0x26,0xb7,0xc3,0xbc,0x39,0x0,0xd0,0x97,0xd1,
  0x2a,0x73,0xf3,0xba,0x73,0x12,0xaa,0x32,0xb3,0x5d,0xe6,0xeb,0x1,0x90,0x79,0xdd,
  0x39,0xde,0xea,0x49,0xfe,0x5f,0xeb,0x4f,0x5a,0x83,0xf0,0x1e,0x16,0x99,0x6e,0xc2,
  0x0,0x0,0x0,0x0,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82,
  
};

static const unsigned char qt_resource_name[] = {
  // images
  0x0,0x6,
  0x7,0x3,0x7d,0xc3,
  0x0,0x69,
  0x0,0x6d,0x0,0x61,0x0,0x67,0x0,0x65,0x0,0x73,
    // about.png
  0x0,0x9,
  0x6,0xc7,0x98,0x67,
  0x0,0x61,
  0x0,0x62,0x0,0x6f,0x0,0x75,0x0,0x74,0x0,0x2e,0x0,0x70,0x0,0x6e,0x0,0x67,
  
};

static const unsigned char qt_resource_struct[] = {
  // :
  0x0,0x0,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x1,
  // :/images
  0x0,0x0,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x2,
  // :/images/about.png
  0x0,0x0,0x0,0x12,0x0,0x0,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x0,

};

QT_BEGIN_NAMESPACE

extern Q_CORE_EXPORT bool qRegisterResourceData
    (int, const unsigned char *, const unsigned char *, const unsigned char *);

extern Q_CORE_EXPORT bool qUnregisterResourceData
    (int, const unsigned char *, const unsigned char *, const unsigned char *);

QT_END_NAMESPACE


int QT_MANGLE_NAMESPACE(qInitResources_qrcexample)()
{
    QT_PREPEND_NAMESPACE(qRegisterResourceData)
        (0x01, qt_resource_struct, qt_resource_name, qt_resource_data);
    return 1;
}

Q_CONSTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qInitResources_qrcexample))

int QT_MANGLE_NAMESPACE(qCleanupResources_qrcexample)()
{
    QT_PREPEND_NAMESPACE(qUnregisterResourceData)
       (0x01, qt_resource_struct, qt_resource_name, qt_resource_data);
    return 1;
}

Q_DESTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qCleanupResources_qrcexample))
除了这个方法外,还有两种方法也可以达到目的(这两种方法生成的qrc_*.cpp会在GeneratedFiles文件夹下)。

1.直接将qrc也删除,然后再建一个qrc,系统会自动生成相应的qrc_*.cpp文件。如果是一个解决方案中含有多个项目时,可能会失败,我就遇到这个问题。

2.如果是一个解决方案中的多个项目,建议将该项目独立打开,然后将qrc删除,然后再添加qrc。我是这样解决的。

另外,在引用qrc资源时要注意,是:/prefix/images/xxx.png.其中冒号是必须的,表明是引用的路径。/prefix是qrc资源文件中设置的前缀。images/xxx.png,是该图片对应qrc的相对路径。当然也可以将/prefix直接换成/,这样引用会方便一点(:/images/xxx.png)。下面是qrc的代码。


    
        images/about.png
    

转载请注明出处 http://blog.csdn.net/xxdddail/article/details/17410371。





你可能感兴趣的:(C++,QT)