简体   繁体   中英

QTranslator not installing persistently in PyQt5

I'm developing an app using PyQt5 and I'm encountering a problem related to the i18n functions of the framework, specifically all the QTranslator related code.

As the docs say, I'm using a QTranslator object to load the compiled .qm file, like this:

tr_object = QTranslator()
tr_object.load(current, "lang")
QCoreApplication.installTranslator(tr_object)

The problem is that it seems to only install it "temporarily", ie if I call translate() immediately afterwards it works, but then it's as if the translator gets uninstalled immediately.

My current (ugly) workaround is to replace the translate() function in QCoreApplication by a wrapper that re-installs the translator every time, but that creates a very large overhead that slows down everything.

I have also tried using my instance of QApplication instead of the static QCoreApplication, same issue. The QT_TR_NOOP macros don't work either.

Qt不拥有翻译器的所有权,因此您需要在python端保留对它的引用,以防止它被垃圾回收。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM