如何使用 cmake 和 QRC 将 Qt4 qm 文件集成为二进制文件?
我有一个 Qt4 CMake 项目,我想将 i18n 的 QM 文件集成到输出二进制文件中。这些是我到目前为止生成 TS 和 QM 文件的规则:
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})
我尝试了以下方法将 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 进行了影子构建(在源目录之外构建),但解析了 QRC 文件以获取依赖项,期望引用的文件相对于 QRC 文件(很好的功能,但没有 make 规则如何构建)该位置的 QM 文件)。 QM 文件位于 ${CMAKE_CURRENT_BINARY_DIR}
中(它们属于使用影子构建的位置),但期望它位于 ${CMAKE_CURRENT_SOURCE_DIR}
中(非生成的文件应该位于其中 - 所以两者位置将是正确的,具体取决于情况)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了完全相同的问题。我想出了以下解决方案:
创建一个仅包含预期 QM 文件的 QRC 文件,并为其指定不同的前缀,这样它就不会与您的其他资源冲突:
添加 CMake 规则以将 QRC 文件复制到输出目录然后运行资源编译器的另一条规则:
如果使用 Qt 5,请使用
${Qt5Core_RCC_EXECUTABLE}
而不是${QT_RCC_EXECUTABLE}
。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:
Add a CMake rule to copy the QRC file to the output directory and then another rule to run the resource compiler:
Use
${Qt5Core_RCC_EXECUTABLE}
instead of${QT_RCC_EXECUTABLE}
if you use Qt 5.我在 CMake 3.0(也许更早)中找到了一种非常简单的方法,没有
ADD_CUSTOM_COMMAND
和其他复杂性。首先,您应该创建一个 QRC 文件,其中列出了所有
.qm
文件(谢谢,the_fly_123< /a> ):然后您可以使用
configure_file
将此 QRC 文件复制到输出目录中,并使用标准 Qt 例程来构建和添加它:然后将
${myapp_QM_RC}
包含在add_executable
以及其他来源。注意:对于 Qt4,请将所有
qt5_
前缀替换为qt4_
I have found a very simple way to do it in CMake 3.0 (and, maybe, earlier) without
ADD_CUSTOM_COMMAND
and other complications.First, you should create a QRC file with all
.qm
files listed ( thanks, the_fly_123 ):Then you can copy this QRC file into the output directory using
configure_file
and use standard Qt routines to build and add it:Then include
${myapp_QM_RC}
inadd_executable
along with other sources.Note: For Qt4 replace all
qt5_
prefixes withqt4_
我的解决方案是从头开始生成带有编译翻译的
ts.qrc
XML 文件,然后将其与 app 进行编译。以下是示例:
文件树:
ts
目录中的文件最初由lupdate
工具生成。My solution is to generate
ts.qrc
XML file with compiled translations from scratch and then complie it with app.Here is example:
File tree:
Files in
ts
dir initially generated bylupdate
tool.您需要使用 Qt 资源系统将翻译直接包含到应用程序二进制文件中。使用 QT4_ADD_RESOURCES 宏来执行此操作。有一些如何使用它的示例: http://www.qtcentre.org /wiki/index.php?title=Compiling_Qt4_apps_with_CMake
You need to use Qt resources system to include your translation directly into your application binary. Use QT4_ADD_RESOURCES macro to do this. There is some example how to use it: http://www.qtcentre.org/wiki/index.php?title=Compiling_Qt4_apps_with_CMake