Documents

qt 资源

作 者:康 林(kl222@126.com)


qt 资源包括图片资源、翻译资源等。

添加资源文件

在项目视图中,在要添加资源的目录上右键。点 “Add New …“,选择 Qt 资源文件。 注意:资源文件名建议使用 $${PROJECT_NAME}_RESOURCE.qrc ; 如下图:

添加资源

在刚才建立的资源文件上双击,或者使用资源编辑器打开。向里面添加资源。资源文件实际上是一个文件文件,你也可以直接编辑。

使用

加入项目

初始化资源

如果要使用库中的资源,则需要在程序开始的时候,用 Q_INIT_RESOURCE 初始化库中的资源,在程序结束时,用 Q_CLEANUP_RESOURCE 清理库中的资源。 如果是程序,Qt 会自动调用 Q_INIT_RESOURCE 初始化资源。但如果是库,则需要手工调用 Q_INIT_RESOURCE 初始化资源。 Q_INIT_RESOURCE 参数为资源文件的基本名。

    Q_INIT_RESOURCE(qApp->applicationName() + "_RESOURCE")

访问资源

在代码中,资源可以用 “:/” 象普通文件一样访问。

    QImage image(":/image/man.png");

释放资源

    Q_CLEANUP_RESOURCE(qApp->applicationName() + "_RESOURCE")

翻译资源

pro

Translations.pri

使用: 把此文件包含进入工程文件中(.pro)

    TRANSLATIONS_DIR =
    TRANSLATIONS_NAME = 
    include(../pri/Translations.pri) 

在代码中加载翻译资源

    Q_INIT_RESOURCE(${TRANSLATIONS_NAME}); //初始化资源
    QTranslator translator;
    translator.load(CRabbitCommonGlobalDir::Instance()->GetDirTranslations()
               + "/" + qApp->applicationName() + "_" + QLocale::system().name() + ".qm");
    qApp->installTranslator(&translator);

debug 翻译资源做为资源文件嵌入程序

其它系统发行模式下,做为文件放在程序的安装目录 Translations 目录下
程序的安装目录:

    AppRoot 
      |- bin
      |    |- App.exe
      |- lib
      |      
      |- translations
          |- ${TRANSLATIONS_NAME}_zh_CN.qm
          |- ${TRANSLATIONS_NAME}_zh_TW.qm

Android 翻译安装目录:

    assets
      |- translations
          |- ${TRANSLATIONS_NAME}_zh_CN.qm
          |- ${TRANSLATIONS_NAME}_zh_TW.qm

源码目录:

    SourceRoot 
      |- App
      |   |- Resource
      |         |-Translations
      |               |- ${TRANSLATIONS_NAME}_zh_CN.ts
      |               |- ${TRANSLATIONS_NAME}_zh_TW.ts
      |- pri
      |      |- Translations.pri
      |      |- lrelease.pri
      |- Src
          |- Resource
               |-Translations
                     |- ${TRANSLATIONS_NAME}_zh_CN.ts
                     |- ${TRANSLATIONS_NAME}_zh_TW.ts
cmake

Translations.cmake

GENERATED_QT_TRANSLATIONS 函数:生成 qt 翻译

debug 翻译资源做为资源文件嵌入程序

android 翻译资源放在 assets 中

    Android:
      assets                                      GetDirApplicationInstallRoot()  (Only read)
        |- translations                           GetDirTranslations()
        |        |- ${TRANSLATIONS_NAME}_zh_CN.ts
        |        |- ${TRANSLATIONS_NAME}_zh_TW.ts

其它系统发行模式下,做为文件放在程序的安装目录 Translations 目录下 程序的安装目录:

    AppRoot |
            |- bin
            |   |- App.exe
            |- lib
            |
            |- translations
                  |- ${TRANSLATIONS_NAME}_zh_CN.qm
                  |- ${TRANSLATIONS_NAME}_zh_TW.qm

源码目录:

    SourceRoot |
               |- App
               |   |- Resource
               |        |-Translations
               |             |- ${TRANSLATIONS_NAME}_zh_CN.ts
               |             |- ${TRANSLATIONS_NAME}_zh_TW.ts
               |- cmake
               |   |- Translations.cmake
               |- Src
                   |- Resource
                       |-Translations
                             |- ${TRANSLATIONS_NAME}_zh_CN.ts
                             |- ${TRANSLATIONS_NAME}_zh_TW.ts