<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to translate text with QUiLoader?]]></title><description><![CDATA[<p dir="auto">Hi guys,<br />
I'm trying to translate the UI texts. The problem is that I use <code>Designer</code> to design my UI, and so I then use QUiLoader to load my ui file. But when I try to use <code>retranslateUi</code>, it just comes with this error: <code>AttributeError: 'PySide6.QtWidgets.QWidget' object has no attribute 'retranslateUi'</code>. Code:</p>
<pre><code>class CustomMessageBox(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = QUiLoader().load('../../message.ui')  # 主界面
        self.setCentralWidget(self.ui)

        self.trans = QTranslator()
        self.trans.load('../../message.qm')
        app = QApplication.instance()
        app.installTranslator(self.trans)
        self.ui.retranslateUi(self)


if __name__ == '__main__':
    from PySide6.QtWidgets import QApplication

    app = QApplication()
    ui = CustomMessageBox()
    ui.show()
    app.exec()
</code></pre>
<p dir="auto">Is it possible to translate the UI for application created by QUiLoader?</p>
]]></description><link>https://forum.qt.io/topic/141742/how-to-translate-text-with-quiloader</link><generator>RSS for Node</generator><lastBuildDate>Fri, 01 May 2026 05:53:05 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/141742.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 26 Dec 2022 12:54:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to translate text with QUiLoader? on Wed, 28 Dec 2022 06:30:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/friedemannkleint">@<bdi>friedemannkleint</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
Thanks for the help. I'm going to succeed. There's still a problem. Codes first:</p>
<pre><code>from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QMainWindow
from PySide6.QtWidgets import QApplication, QMenu, QMessageBox
from PySide6.QtGui import QCursor, QAction
from PySide6.QtCore import QTranslator

class PP(QMainWindow):
    def __init__(self):
        super().__init__()

        loader = QUiLoader()
        loader.setLanguageChangeEnabled(True)
        print(loader.isLanguageChangeEnabled())

        self.app = QApplication.instance()

        self.main_ui = loader.load('test.ui')
        self.setCentralWidget(self.main_ui)
        self.main_ui.pushButton.clicked.connect(self.trans_ui)

        self.menu = QMenu(self)
        self.action = QAction(self.tr('hello'), self)
        self.action.triggered.connect(self.trans_code)
        self.menu.addAction(self.action)
        self.main_ui.pushButton.customContextMenuRequested.connect(self.show_menu)

        self.show()

    def changeEvent(self, event):
        print(event)

    def load_qm(self):
        self.trans_window = QTranslator()
        self.trans_window.load('test.qm')
        self.app.installTranslator(self.trans_window)
        
        self.trans_main = QTranslator()
        self.trans_main.load('ttt.qm')
        self.app.installTranslator(self.trans_main)

    def trans_ui(self):
        self.load_qm()

    def trans_code(self):
        self.main_ui.label.setText(self.tr('pear'))
        QMessageBox.information(self, self.tr('cat'), self.tr('apple'))

    def show_menu(self):
        self.menu.exec(QCursor.pos())

if __name__ == '__main__':
    app = QApplication()
    ui = PP()
    app.exec()
</code></pre>
<p dir="auto">The related ts file:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/977af764-4b57-445c-b93e-bf318a11a7bc.png" alt="bd22e1d3-7cfc-4b38-993c-9d01907d813b-image.png" class=" img-fluid img-markdown" /><br />
There are a button "translate" and a label. Right click the button, a menu will appear. And then left click the 'hello' action, a QMessage box will appear and text 'pear' will be set to the label. I hope that before I left click the button, all the text are English. And after  I left click the button, all the text can be translated to Chinese directly.<br />
But in the below GIF:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/885d53d4-0c33-4f85-a3aa-43fb48efe8c5.gif" alt="case3.gif" class=" img-fluid img-markdown" /><br />
Problem 1: after  I left click the button, the  action text 'hello' was not translated. The label text 'pear' was translated afterI left click the action. (I hope it can be translated as soon as I left click the button.)</p>
<p dir="auto">If I change the code as follows, which means load 'ttt.qm' before <code>load()</code>  ui file just as what <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said.</p>
<pre><code>class PP(QMainWindow):
    def __init__(self):
        super().__init__()

        loader = QUiLoader()
        loader.setLanguageChangeEnabled(True)
        print(loader.isLanguageChangeEnabled())

        self.app = QApplication.instance()

        self.trans_main = QTranslator()
        self.trans_main.load('ttt.qm')
        self.app.installTranslator(self.trans_main)

        self.main_ui = loader.load('test.ui')
        self.setCentralWidget(self.main_ui)
        self.main_ui.pushButton.clicked.connect(self.trans_ui)
        ......

    ......

    def load_qm(self):
        self.trans_window = QTranslator()
        self.trans_window.load('test.qm')
        self.app.installTranslator(self.trans_window)

    def trans_ui(self):
        self.load_qm()
  
    ......
</code></pre>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/cf9882cf-ac00-467d-9d50-64f8f0669874.gif" alt="case4.gif" class=" img-fluid img-markdown" /><br />
All text in 'ttt.ts' can be translated at the beginning, but this is not what I want.<br />
Problem 2: what's the difference between this two case? I'm puzzled on what the code is doing.</p>
]]></description><link>https://forum.qt.io/post/741664</link><guid isPermaLink="true">https://forum.qt.io/post/741664</guid><dc:creator><![CDATA[feiyuhuahuo]]></dc:creator><pubDate>Wed, 28 Dec 2022 06:30:23 GMT</pubDate></item><item><title><![CDATA[Reply to How to translate text with QUiLoader? on Wed, 28 Dec 2022 03:19:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
So I need to add code to translate each widget by myself just like what <code>retranslateUi()</code> does? I have quite a lot of widgets. I prefer to keep my code short and clean. So I will try some other ways. Thanks for the reply.<br />
By the way,  according to <a class="plugin-mentions-user plugin-mentions-a" href="/user/friedemannkleint">@<bdi>friedemannkleint</bdi></a> , I've already done some successful test. But still, there's some problem. Would like to take a look?</p>
]]></description><link>https://forum.qt.io/post/741651</link><guid isPermaLink="true">https://forum.qt.io/post/741651</guid><dc:creator><![CDATA[feiyuhuahuo]]></dc:creator><pubDate>Wed, 28 Dec 2022 03:19:32 GMT</pubDate></item><item><title><![CDATA[Reply to How to translate text with QUiLoader? on Tue, 27 Dec 2022 10:47:23 GMT]]></title><description><![CDATA[<p dir="auto">Provided all the .qm are created and loaded properly, QUiLoader should retranslate on receiving QEvent::LanguageChange if QUiLoader.setLanguageChangeEnabled() was set to true.</p>
<p dir="auto">See <a href="https://doc.qt.io/qtforpython-6/tutorials/basictutorial/translations.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qtforpython-6/tutorials/basictutorial/translations.html</a><br />
and <a href="https://doc.qt.io/qtforpython-6/PySide6/QtUiTools/QUiLoader.html?highlight=quiloader#PySide6.QtUiTools.PySide6.QtUiTools.QUiLoader.setLanguageChangeEnabled" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qtforpython-6/PySide6/QtUiTools/QUiLoader.html?highlight=quiloader#PySide6.QtUiTools.PySide6.QtUiTools.QUiLoader.setLanguageChangeEnabled</a></p>
<p dir="auto">Note we generally recommend using pyside6-uic for Qt Designer forms (see <a href="https://doc.qt.io/qtforpython-6/tutorials/basictutorial/uifiles.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qtforpython-6/tutorials/basictutorial/uifiles.html</a> ).</p>
]]></description><link>https://forum.qt.io/post/741574</link><guid isPermaLink="true">https://forum.qt.io/post/741574</guid><dc:creator><![CDATA[friedemannkleint]]></dc:creator><pubDate>Tue, 27 Dec 2022 10:47:23 GMT</pubDate></item><item><title><![CDATA[Reply to How to translate text with QUiLoader? on Tue, 27 Dec 2022 09:51:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/feiyuhuahuo">@<bdi>feiyuhuahuo</bdi></a><br />
So you want to be able to re-change the language dynamically at runtime.  I don't think using <code>QUiLoader().load()</code> in itself prevents this.  It just means that you do not have individual variables pointing to each widget which should be translated, rather any time you want to access widgets you have to walk the hierarchy via <code>QObject.findChildren&lt;QWidget&gt;</code>.  You then need to know which ones (all of them?) you want translated and get the translation.</p>
<p dir="auto">I don't do translation, but one problem I see: I <em>believe</em> the <em>original</em> English (or whatever you type into the Designer) gets <em>completely replaced</em> by the translation (e.g. in <code>QUiLoader().load()</code>).  So I don't know when you want to re-translate you are supposed to find the <em>original</em> text to lookup from?</p>
<p dir="auto">You might want to try running <code>pyuic</code> (or whatever) on your project or a sample one just to look at the code it generates for <code>retranslateUi()</code>.  You will see it's nothing "magic", but has the advantage if called again that it knows exactly which widgets need translating.</p>
<p dir="auto">Google also for something like <code>qt translation dynamic</code> to see what work you have to do, e.g. an old link is <a href="https://forum.qt.io/topic/55200/dynamic-translation-of-strings-in-widgets">https://forum.qt.io/topic/55200/dynamic-translation-of-strings-in-widgets</a>.</p>
]]></description><link>https://forum.qt.io/post/741561</link><guid isPermaLink="true">https://forum.qt.io/post/741561</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 27 Dec 2022 09:51:20 GMT</pubDate></item><item><title><![CDATA[Reply to How to translate text with QUiLoader? on Tue, 27 Dec 2022 01:34:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<pre><code>        self.trans = QTranslator()
        self.trans.load('../../message.qm')
        app = QApplication.instance()
        app.installTranslator(self.trans)
</code></pre>
<p dir="auto">I move the four lines before <code>QUiLoader().load()</code>, and yes, it works. The UI can be translated successfully. But still, there's a problem. If I want to change the language real-timely，is it possble? I mean that in my UI there are several buttons. Each button means a certain language. By clicking these buttons, the UI can directly update the language without restart the APP. If so, seems I must use <code>retranslateUi</code> function. But I can't get it if use <code>QUiLoader().load()</code>.</p>
]]></description><link>https://forum.qt.io/post/741537</link><guid isPermaLink="true">https://forum.qt.io/post/741537</guid><dc:creator><![CDATA[feiyuhuahuo]]></dc:creator><pubDate>Tue, 27 Dec 2022 01:34:06 GMT</pubDate></item><item><title><![CDATA[Reply to How to translate text with QUiLoader? on Mon, 26 Dec 2022 13:35:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/starterkit">@<bdi>StarterKit</bdi></a> said in <a href="/post/741478">How to translate text with QUiLoader?</a>:</p>
<blockquote>
<p dir="auto">But QUiLoader.load() returns QWidget so should type-casting be there?...</p>
</blockquote>
<p dir="auto">Don't know what you mean here.  You are Python, so what "type-casting"??</p>
<p dir="auto"><code>retranslateUi()</code> is not a method on any widget.  It is a little function which is produced by running the "<strong>pyuic</strong>"-type program for what PySide/PyQt you use on the <code>.ui</code> file produced by the designer.  But you don't run these, so I don't think you have one anywhere.  See e.g. <a href="https://stackoverflow.com/questions/66361580/what-does-retranslateui-do-in-pyqt" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/66361580/what-does-retranslateui-do-in-pyqt</a>.</p>
]]></description><link>https://forum.qt.io/post/741483</link><guid isPermaLink="true">https://forum.qt.io/post/741483</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 26 Dec 2022 13:35:05 GMT</pubDate></item><item><title><![CDATA[Reply to How to translate text with QUiLoader? on Mon, 26 Dec 2022 13:11:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> good point.<br />
<code>QWidget</code> indeed has no <code>retranslateUi()</code> and it is <code>uic</code> who adds it to the class when it is created from ui-file.<br />
So either your are right or it isn't possible.</p>
<p dir="auto">But <code>QUiLoader.load()</code> returns <code>QWidget</code> so should type-casting be there?...</p>
]]></description><link>https://forum.qt.io/post/741478</link><guid isPermaLink="true">https://forum.qt.io/post/741478</guid><dc:creator><![CDATA[StarterKit]]></dc:creator><pubDate>Mon, 26 Dec 2022 13:11:17 GMT</pubDate></item><item><title><![CDATA[Reply to How to translate text with QUiLoader? on Mon, 26 Dec 2022 13:06:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/feiyuhuahuo">@<bdi>feiyuhuahuo</bdi></a><br />
I <em>wonder</em> whether <code>QUiLoader().load()</code> already respects current translator and translates for you??  What if you move your translator stuff to <em>before</em> the <code>load()</code>??</p>
]]></description><link>https://forum.qt.io/post/741477</link><guid isPermaLink="true">https://forum.qt.io/post/741477</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 26 Dec 2022 13:06:24 GMT</pubDate></item></channel></rss>