如何使用cmake和QRC将Qt4 qm文件集成到二进制文件中? [英] How to Integrate Qt4 qm files into binary using cmake and QRC?

查看:644
本文介绍了如何使用cmake和QRC将Qt4 qm文件集成到二进制文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Qt4 CMake项目,我想将i18n的QM文件集成到输出二进制。这些是我迄今为止生成TS和QM文件的规则:

 设置(myapp_TRANSLATIONS 
i18n / myapp_de .ts


set(FILES_TO_TRANSLATE
$ {myapp_SRCS}
$ {myapp_MOC_HDRS}


QT4_CREATE_TRANSLATION(QM_FILES $ {FILES_TO_TRANSLATE} $ {myapp_TRANSLATIONS})
QT4_ADD_TRANSLATION(QM $ {myapp_TRANSLATIONS})

我尝试以下将QM文件添加到可执行文件:

  add_executable(myapp $ {myapp_SRCS} $ {myapp_MOC_SRCS} myapp_RCC_SRCS} $ {QM})

这是从main.cpp初始化:

  QTranslator qtTranslator; 
qtTranslator.load(qt_+ QLocale :: system()。name(),QLibraryInfo :: location(QLibraryInfo :: TranslationsPath));
app.installTranslator(& qtTranslator);

QTranslator appTranslator;
appTranslator.load(myapp_+ QLocale :: system()。name());
app.installTranslator(& appTranslator);

但是, strings mypp

更新:我将每个qm文件添加到 i18n / translations.qrc

 <!DOCTYPE RCC>< RCC version =1.0> 
< qresource prefix =/ resources>
< file> myapp_de.qm< / file>
< file> ... .qm< / file>
< / qresource>
< / RCC>

并使用

  QT4_ADD_RESOURCES(myapp_QM_RCC_SRCS i18n / translations.qrc)

并添加 myapp_QM_RCC_SRCS 到可执行文件的依赖关系。



但是这在构建时失败,这是由于CMake做了一个阴影构建dir),但解析QRC文件的依赖性,期望相对于QRC文件的引用文件(漂亮的功能,但没有make规则如何在该位置构建QM文件)。 QM文件在 $ {CMAKE_CURRENT_BINARY_DIR} (它们属于使用阴影构建),但期望它在 $ {CMAKE_CURRENT_SOURCE_DIR}

解决方案

我有一个非生成的文件,完全相同的问题。我想出了以下解决方案:



创建一个只包含预期的QM文件的QRC文件,并给它一个不同的前缀,所以它不会与您的其他资源:

 < RCC> 
< qresource prefix =/ translators>
< file> myapp_en.qm< / file>
< / qresource>
< / RCC>

添加CMake规则将QRC文件复制到输出目录,然后再运行另一个规则编译器:

 #将'myapp_en'改为qrc文件的基本文件名。 
SET(trans_file myapp_en)
SET(trans_srcfile $ {CMAKE_CURRENT_SOURCE_DIR} / $ {trans_file} .qrc)
SET(trans_infile $ {CMAKE_CURRENT_BINARY_DIR} / $ {trans_file} .qrc)
SET(trans_outfile $ {CMAKE_CURRENT_BINARY_DIR} / qrc _ $ {trans_file} .cxx)

#将QRC文件复制到输出目录,因为
#qrc文件中列出的文件是相对的到该目录。
ADD_CUSTOM_COMMAND(
OUTPUT $ {trans_infile}
COMMAND $ {CMAKE_COMMAND} -E copy $ {trans_srcfile} $ {trans_infile}
MAIN_DEPENDENCY $ {trans_srcfile}


#运行资源编译器(应该已经设置了rcc_options)。我们不能
#使用QT4_ADD_RESOURCES,因为qrc文件可能不存在。
ADD_CUSTOM_COMMAND(
OUTPUT $ {trans_outfile}
COMMAND $ {QT_RCC_EXECUTABLE}
ARGS $ {rcc_options} -name $ {trans_file} -o $ {trans_outfile} $ {trans_infile}
MAIN_DEPENDENCY $ {trans_infile}
DEPENDS $ {qm_files}


#将编译资源添加到可执行依赖项列表
ADD_EXECUTABLE($ {APP_NAME} 。$ {trans_outfile})

使用 $ {Qt5Core_RCC_EXECUTABLE} 而不是 $ {QT_RCC_EXECUTABLE}


I have a Qt4 CMake project and I'd like to integrate the QM files for i18n into the output binary. These are the rules I have so far for generating the TS and QM files:

set(myapp_TRANSLATIONS
    i18n/myapp_de.ts
)

set(FILES_TO_TRANSLATE
    ${myapp_SRCS}
    ${myapp_MOC_HDRS}
)

QT4_CREATE_TRANSLATION(QM_FILES ${FILES_TO_TRANSLATE} ${myapp_TRANSLATIONS})
QT4_ADD_TRANSLATION(QM ${myapp_TRANSLATIONS})

I tried the following to add the QM files to the executable:

add_executable(myapp ${myapp_SRCS} ${myapp_MOC_SRCS} ${myapp_RCC_SRCS} ${QM})

This is the initialization from main.cpp:

QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
app.installTranslator(&qtTranslator);

QTranslator appTranslator;
appTranslator.load("myapp_" + QLocale::system().name());
app.installTranslator(&appTranslator);

However, strings mypp shows that the translations are not going into the binary.

Update: I added each qm file to a i18n/translations.qrc:

<!DOCTYPE RCC><RCC version="1.0">
  <qresource prefix="/resources">
    <file>myapp_de.qm</file>
    <file>  ...   .qm</file>
  </qresource>
</RCC>

and using

QT4_ADD_RESOURCES(myapp_QM_RCC_SRCS i18n/translations.qrc)

and adding myapp_QM_RCC_SRCS to the executable dependencies.

But this fails during build time thanks to the fact that CMake does a shadow build (building outside the source dir) but parses the QRC files for dependencies expecting the referenced files relative to the QRC file (nice feature but there's no make rule how to build the QM file at that location). The QM files are in ${CMAKE_CURRENT_BINARY_DIR} (where they belong using shadow building) but expects it in ${CMAKE_CURRENT_SOURCE_DIR} (where non-generated files should be - so both locations would be correct, depending on situation).

解决方案

I had the exact same problem. I came up with the following solution:

Create a QRC file that contains only the expected QM files, and give it a different prefix so it won't conflict with your other resources:

<RCC>
    <qresource prefix="/translators">
    <file>myapp_en.qm</file>
    </qresource>
</RCC>

Add a CMake rule to copy the QRC file to the output directory and then another rule to run the resource compiler:

# Change 'myapp_en' to be the base file name of the qrc file.
SET( trans_file myapp_en )
SET( trans_srcfile ${CMAKE_CURRENT_SOURCE_DIR}/${trans_file}.qrc)
SET( trans_infile ${CMAKE_CURRENT_BINARY_DIR}/${trans_file}.qrc)
SET( trans_outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${trans_file}.cxx)

# Copy the QRC file to the output directory, because the files listed in the 
# qrc file are relative to that directory.
ADD_CUSTOM_COMMAND(
    OUTPUT ${trans_infile}
    COMMAND ${CMAKE_COMMAND} -E copy ${trans_srcfile} ${trans_infile}
    MAIN_DEPENDENCY ${trans_srcfile}
    )

# Run the resource compiler (rcc_options should already be set). We can't
# use QT4_ADD_RESOURCES because the qrc file may not exist yet.
ADD_CUSTOM_COMMAND(
    OUTPUT ${trans_outfile}
    COMMAND ${QT_RCC_EXECUTABLE}
    ARGS ${rcc_options} -name ${trans_file} -o ${trans_outfile} ${trans_infile}
    MAIN_DEPENDENCY ${trans_infile}
    DEPENDS ${qm_files}
    )

# Add compiled resources to executable dependency list
ADD_EXECUTABLE( ${APP_NAME} ... ${trans_outfile} )

Use ${Qt5Core_RCC_EXECUTABLE} instead of ${QT_RCC_EXECUTABLE} if you use Qt 5.

这篇关于如何使用cmake和QRC将Qt4 qm文件集成到二进制文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆