Since PySide is a derivative of Qt, a tool called Qt Designer is useful when developing GUIs. This is a so-called RAD tool that allows you to develop your GUI visually. As a result, a ".ui file" is created, but there is a slight habit of handling it from within the PySide source code (.py file) (different from Qt and PyQt), so create an article as a memorandum. I will do it.
You can use the part integrated with QtCreater, but if you want to use QtDesigner alone, it will be in the following directory. How to use QtDesigner is more familiar than learning, and there are many detailed explanations from the Qt area, so I will omit it.
C:\Python\Lib\site-packages\PySide\designer.exe
The simplest form is as follows. It is assumed that a GUI description file called dialog.ui has been created. The flow is to read the .ui file with QUiLoader and display it with the show () function.
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtUiTools import QUiLoader
class Dialog(QDialog):
def __init__(self, parent = None):
super(Dialog, self).__init__(parent)
#With QUiLoader.Read ui file
self.dialogUi = QUiLoader().load("./dialog.ui")
if __name__ == '__main__':
#Create a Qt Application
app = QApplication(sys.argv)
#Create and display form
mainWin = Dialog()
mainWin.dialogUi.show()
#Start the main loop of Qt
sys.exit(app.exec_())
Specify the editable object name in the upper right corner of QtDesigner.
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtUiTools import QUiLoader
class Dialog(QDialog):
def __init__(self, parent = None):
super(Dialog, self).__init__(parent)
self.dialogUi = QUiLoader().load("./dialog.ui")
#Set label text
self.dialogUi.label.setLabel("Hello!")
#Change label text at the push of a button
self.dialogUi.pushButton.clicked.connect(lambda: self.dialogUi.label.setLabel("World!"))
if __name__ == '__main__':
#Create a Qt Application
app = QApplication(sys.argv)
#Create and display form
mainWin = Dialog()
mainWin.dialogUi.show()
#Start the main loop of Qt
sys.exit(app.exec_())
It is as follows.
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtUiTools import QUiLoader
class Dialog(QDialog):
def __init__(self, parent = None):
super(Dialog, self).__init__(parent)
self.dialogUi = QUiLoader().load("./dialog.ui")
self.dialogUi.label.setText("Hello!")
self.dialogUi.pushButton.clicked.connect(lambda: self.dialogUi.label.setText("World!"))
#Load ui file with QUiLoader
self.widgetUi = QUiLoader().load("./widget.ui")
#Add a widget to the main layout of the dialog
self.dialogUi.verticalLayout.addWidget(self.widgetUi)
if __name__ == '__main__':
#Create a Qt Application
app = QApplication(sys.argv)
#Create and display form
mainWin = Dialog()
mainWin.dialogUi.show()
#Start the main loop of Qt
sys.exit(app.exec_())
That's all for handling .ui files in PySide.
Actually, there is one problem with GUI settings using .ui files in PySide.
Add self.show ()
inside the \ _ \ _ init \ _ \ _ () function, or if \ _ \ _ name \ _ \ _ =='\ _ \ _ main \ _ \ _': main loop If you add mainWin.show ()
inside, a clean window will be displayed separately from the GUI created by QtDesigner. This is the GUI of the Dialog class itself, which was originally created by inheriting the QDialog class, and at present (PySide 1.2.2), there seems to be no way to apply the .ui file here.
It seems that PySide2 is in the middle of development to support Python3 and Qt5, so I hope it will be improved as well.
Recommended Posts