I'm facing an issue. I use Qt Linguist for multi language app. If use for QWidget, it work well. But when i use with QTabWidget, it just change language in one tab, other tab do not update language. Anyone help me, please
Example: I have button in tab 1, when i press that button, i want to change language in all tab. But just tab 1 change, other tab do not change. See my code
My Main Window
import os
import sys
from PySide6.QtCore import Qt, QLocale, QTranslator, QCoreApplication
from PySide6.QtWidgets import QWidget, QMainWindow, QApplication, QVBoxLayout, QLabel, QPushButton, QTabWidget
from screen_one import ScreenOne
from src.screen_two import ScreenTwo
from src.tab_one import TabOne
from src.tab_two import TabTwo
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.current_locale = QLocale.system().name()
self.load_ui()
def load_ui(self):
self.central_widget = QWidget()
self.central_layout = QVBoxLayout()
self.central_widget.setLayout(self.central_layout)
self.tab_widget = QTabWidget()
self.screen_one = ScreenOne()
self.screen_one.click_signal.connect(self.change_language)
self.screen_two = ScreenTwo()
self.tab_one = TabOne()
self.tab_two = TabTwo()
# Add tabs to the tab widget
self.tab_widget.addTab(self.screen_one, "Tab 1")
self.tab_widget.addTab(self.screen_two, "Tab 2")
self.tab_widget.addTab(self.tab_one, "Tab 3")
self.tab_widget.addTab(self.tab_two, "Tab 4")
self.central_layout.addWidget(self.tab_widget)
self.setCentralWidget(self.central_widget)
def change_language(self):
qtranslator = QTranslator()
translator.load("/path/main_vi.qm")
QCoreApplication.instance().installTranslator(qtranslator)
self.retranslateUi()
def retranslateUi(self):
# Update the text of the label and button
for index in range(self.tab_widget.count()):
self.tab_widget.widget(index).retranslateUi()
if __name__ == "__main__":
app = QApplication(sys.argv)
# Create and install the translator
translator = QTranslator()
translator.load("/path/main_en.qm")
app.installTranslator(translator)
window = MainWindow()
window.show()
sys.exit(app.exec())
My Screen One, which have button to change language:
import sys
from PySide6.QtGui import QImage
from PySide6.QtCore import Qt, QCoreApplication, Signal
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QHBoxLayout, QPushButton
class ScreenOne(QWidget):
click_signal = Signal(bool)
def __init__(self, parent=None):
super().__init__(parent)
self.load_ui()
def load_ui(self):
# create label
self.mylabel = QLabel(self.tr("This is screen 1"))
# center
self.mylabel.setAlignment(Qt.AlignCenter)
self.label_2 = QLabel(self.tr("Hello 1"))
# create label
self.mylabel = QLabel(self.tr("Qt Init Widget"))
# center
self.mylabel.setAlignment(Qt.AlignCenter)
self.label_1 = QLabel(self.tr("This is a QLabel 1"))
self.button_1 = QPushButton(self.tr("Press me 1"))
self.button_2 = QPushButton(self.tr("Press me 2"))
self.button_3 = QPushButton(self.tr("Press me 3"))
self.button_change_language = QPushButton(self.tr("Change Language"))
self.button_change_language.clicked.connect(self.change_language)
# create layout
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignTop)
self.layout.addWidget(self.mylabel)
self.layout.addWidget(self.label_2)
self.layout.addWidget(self.label_1)
self.layout.addWidget(self.button_1)
self.layout.addWidget(self.button_2)
self.layout.addWidget(self.button_3)
self.layout.addWidget(self.button_change_language)
self.setLayout(self.layout)
def change_language(self):
self.click_signal.emit(True)
def retranslateUi(self):
self.mylabel.setText(QCoreApplication.translate("", u"This is screen 1", None))
self.label_2.setText(QCoreApplication.translate("", u"Hello 1", None))
self.label_1.setText(QCoreApplication.translate("", u"This is a QLabel 1", None))
self.button_1.setText(QCoreApplication.translate("", "Press me 1", None))
self.button_2.setText(QCoreApplication.translate("", "Press me 2", None))
self.button_3.setText(QCoreApplication.translate("", "Press me 3", None))
self.button_change_language.setText(QCoreApplication.translate("", "Change Language", None))
My Screen Two, Other Screen similar with Screen two: import sys
from PySide6.QtGui import QImage
from PySide6.QtCore import Qt, QCoreApplication
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QHBoxLayout
class ScreenTwo(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.load_ui()
def load_ui(self):
# create label
self.mylabel = QLabel(self.tr("This is screen 2"))
# center
self.mylabel.setAlignment(Qt.AlignCenter)
self.label_2 = QLabel(self.tr("Hello 2"))
# create layout
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignTop)
self.layout.addWidget(self.mylabel)
self.layout.addWidget(self.label_2)
self.setLayout(self.layout)
def retranslateUi(self):
self.mylabel.setText(QCoreApplication.translate("", u"This is screen 2", None))
self.label_2.setText(QCoreApplication.translate("", u"Hello 2", None))
I found solution. That's my bad, in retranslateUi() function, must pass the context parameter.
Similar for other screen.