gpt4 book ai didi

c++ - 翻译在 Qt 中不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:52 24 4
gpt4 key购买 nike

我一整天都在谷歌上搜索解决方案并更改我的代码,但没有成功。

基本上,我已将翻译添加到我的应用程序中。它工作正常,除了这里:

QString MainWindow::getMessage(Messages msg) {
static const char* const messages[] = {
QT_TR_NOOP("Setting power on"),
QT_TR_NOOP("Reading ID..."),
QT_TR_NOOP("Programming..."),
QT_TR_NOOP("Setting write-protect"),
QT_TR_NOOP("Finished ok"),
QT_TR_NOOP("PROGRAMMED OK"),
QT_TR_NOOP("ERROR!"),
QT_TR_NOOP("OK"),
QT_TR_NOOP("The programmer is not connected.\nPlease check the connection."),
QT_TR_NOOP("Disconnect the board, it is in short!!"),
QT_TR_NOOP("ERROR: Supply voltage too low (1 Volt is required, Measured: 0.0 Volt)."),
QT_TR_NOOP("Board is already programmed and write protected."),
QT_TR_NOOP("Check device connection or there is short."),
QT_TR_NOOP("Unknown error.")
};

return tr(messages[msg]);
}

但是,我没有得到翻译。翻译文件似乎没问题,UI 翻译应用得很好。我也试过这个:

static const char* const messages[] = {
QT_TRANSLATE_NOOP("test", "Setting power on"),
QT_TRANSLATE_NOOP("test", "Reading ID..."),
QT_TRANSLATE_NOOP("test", "Programming..."),
QT_TRANSLATE_NOOP("test", "Setting write-protect"),
QT_TRANSLATE_NOOP("test", "Finished ok"),
QT_TRANSLATE_NOOP("test", "PROGRAMMED OK"),
QT_TRANSLATE_NOOP("test", "ERROR!"),
QT_TRANSLATE_NOOP("test", "OK"),
QT_TRANSLATE_NOOP("test", "The programmer is not connected.\nPlease check the connection."),
QT_TRANSLATE_NOOP("test", "Disconnect the board, it is in short!!"),
QT_TRANSLATE_NOOP("test", "ERROR: Supply voltage too low (1 Volt is required, Measured: 0.0 Volt)."),
QT_TRANSLATE_NOOP("test", "Board is already programmed and write protected."),
QT_TRANSLATE_NOOP("test", "Check device connection or there is short."),
QT_TRANSLATE_NOOP("test", "Unknown error.")
};

然后,我有一个获取消息的方法:

QString MainWindow::getMessage(Messages msg) {
return qApp->translate("test", messages[msg]);
}

但它也不起作用。

有什么提示或建议吗?

最佳答案

我在这里找到了真正的问题。通常翻译器安装在 main.cpp 中,因此翻译器对象保留在内存中。但是,就我而言,我是在用户在设置对话框中更改语言后切换翻译器,使用局部变量但 void QCoreApplication::installTranslator ( QTranslator * translationFile ) [static] 需要一个指针。一旦函数退出,该局部变量就会被删除。所以,我在我的类上声明了一个 QTranslator 对象,所以它保存在内存中。

关于c++ - 翻译在 Qt 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28509106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com