I'm creating a simple window with a combobox to let user choose the text language to be displayed throughout the app
I have created the necessary .qm files, and the text is being updated when I start the aplication. But how can I link this to the options on the combobox, and change the language dinamically from within the mainWindow?
My code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
GUI
'''
import sys
import os.path as osp
import os
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow,self).__init__()
# Set MainWindow geometry, use settings of last session. If it's first session,
# use defaulted settings
self.settings = QtCore.QSettings('Paul',QtCore.QSettings.NativeFormat)
self.resize(self.settings.value("size", QtCore.QSize(500, 300)).toSize())
self.move(self.settings.value("pos", QtCore.QPoint(5, 5)).toPoint());
self.initUI()
def closeEvent(self, e):
#Save MainWindow geometry session when closing the window
self.settings.setValue("size",self.size())
self.settings.setValue("pos",self.pos())
e.accept()
def initUI(self):
self.hbox = QtGui.QVBoxLayout(self) # Create Vertival box layout to put the buttons
self.myButtons = QtGui.QPushButton('button',self) #create push button
self.myButtons.setStyleSheet("""QPushButton { background-color: red; font:bold 20px}""")
self.myButtons.setToolTip('Push this button')
self.myButtons.setText(self.tr('yes'))
comboBox=QtGui.QComboBox(self) #create drop down menu
comboBox.addItem('English')
comboBox.addItem('Portugues')
self.hbox.addWidget(comboBox,1,QtCore.Qt.AlignRight) #add drop down menu to box layout
self.hbox.addStretch(3) # set separation between buttons
self.hbox.addWidget(self.myButtons,1,QtCore.Qt.AlignRight) #add button to box layout
self.setWindowTitle('Test2')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
translator = QtCore.QTranslator()
translator.load("~/translations/qt_pt.qm")
app.installTranslator(translator)
app.setWindowIcon(QtGui.QIcon(path))
ex = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Basically I would like to have the combobox do something like this:
self.comboBox.currentIndexChanged.connect(self.combochange)
def combochange(self):
if self.comboBox.currentText()=='Portugues':
translator = QtCore.QTranslator()
translator.load('~/translations/qt_pt.qm')
app.installTranslator(translator) #Obviously this doesn't work
I assume I have to somehow pass an argument from the mainWindow to the main() function and reload everything.
Is this even possible?
Python 2.7, QT 5.9.1, PyQt4 4.12.1 on OSX 10.11.6
EDIT:
I found this post on QT wiki page that does what I want, unfortunately I am not proficient in C, at all. If someone could help me translate this to python I would be greatly appreciated.
Ok, I found a way to do it, I don't know if it is a very "pythonic" solution, but it works
my main() function now looks like this:
To now which language my aplication needs to load, following user input in the combobox I defined my "combochange" function like so:
"
lang
" is passed outside mymainWindow
, which triggers one application to close and be reopened with a different language.It doesn't look very elegant, but it works.
The full code is: