Qt:我无法在 QApplication 的子类中安装翻译器
在我的应用程序中,我对 QApplication 进行子类化以执行很多操作,例如集中式异常处理、SQLite 数据库连接、通过 QSettings 初始化默认设置(需要时)。特别是加载翻译。我在构造函数中这样做,当我这样做时:
installTranslator (&translator);
qDebug() << "Translation from PApplication:"
<< translate ("PAddFormWindow", "Dialog");
我得到了非常正确的翻译(俄语)。但在我的应用程序中,我看到的所有内容都是英文的。更糟糕的是,即使我在初始化应用程序对象后尝试立即获取翻译,我也会看到它是英文的:
PApplication a ("PCorrect", argc, argv);
qDebug() << "Translation from void main():"
<< a.translate ("PAddFormWindow", "Dialog");
那么,为什么它不起作用?是因为我在构造函数中这样做吗?我还应该提到 PApplication 类不是在应用程序本身中声明和定义的,而是在动态加载的库中声明和定义的(我正在开发一个应用程序套件)。我的平台是带有 Qt 4.7.0 的 Windows XP。
In my app, I'm subclassing QApplication to do a lot of things, like centralized exception handling, SQLite database connection, initializing default settings (when needed) via QSettings. And, in particular, loading translations. I'm doing it in constructor, and when I do:
installTranslator (&translator);
qDebug() << "Translation from PApplication:"
<< translate ("PAddFormWindow", "Dialog");
I get back very right translation (to russian). But in my app I see everything in english. What's worse, I see it in english even when I try to get translation immediatelly after initializing my application object:
PApplication a ("PCorrect", argc, argv);
qDebug() << "Translation from void main():"
<< a.translate ("PAddFormWindow", "Dialog");
So, why won't it work? Is it because I'm doing it in constructor? I should also mention that PApplication class is declared and defined not in the app itself, but in a dynamically loaded library (I'm developing an application suite). And that my platform is Windows XP with Qt 4.7.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
installTranslator(&translator)
中的&translator
可能引用了一个局部变量,该变量在退出构造函数时被销毁。如果 installTranslator() 没有复制它(文档在这个主题上不是很清楚),任何事情都可能发生,包括无效的翻译甚至段错误。事实上,文档给了我们一个提示:“安装或删除 QTranslator,或更改已安装的 QTranslator 会生成 LanguageChange 事件”。我们可以更改翻译器并且它会影响某些内容,这一事实表明它没有被复制。但目前还很不清楚。应该明确说明是否抄袭以及如何处理。
It could be that
&translator
ininstallTranslator(&translator)
refers to a local variable that is destroyed upon exit from the constructor. If installTranslator() doesn't copy it (docs aren't very clear on this topic), anything could happen including non-working translations or even segfaults.In fact, docs give us a hint: "Installing or removing a QTranslator, or changing an installed QTranslator generates a LanguageChange event". The fact that we can change a translator and it will affect something suggests that it isn't copied. But it is still very unclear. It should have said explicitly whether it is copied or not and how we should handle it.