开发者

Qt: I fail to install translator in subclass of QApplication

开发者 https://www.devze.com 2023-01-31 14:33 出处:网络
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 part

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.


It could be that &translator in installTranslator(&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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号