[PYTHON] How to specify a .ui file in the dialog / widget GUI in PySide

Introduction

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

Set the dialog GUI with a .ui file

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_())

Operate GUI parts described in the .ui file

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_())

Set Wijed's GUI in a .ui file and add it to the dialog

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_())

At the end

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

How to specify a .ui file in the dialog / widget GUI in PySide
How to specify a .py file to load at startup in IPython 0.13
[LPIC 101] How to specify the disk partition number in the GRUB configuration file
How to read a file in a different directory
How to specify a schema in Django's database settings
How to write a GUI using the maya command
How to display the modification date of a file in C language up to nanoseconds
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
How to generate a query using the IN operator in Django
How to get the last (last) value in a list in Python
How to create a config file
Output the key list included in S3 Bucket to a file
[sh] How to store the command execution result in a variable
How to determine the existence of a selenium element in Python
How to get all the possible values in a regular expression
PySide --do something in the background while displaying a modeless dialog
How to make a command to read the configuration file with pyramid
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
[Beginner memo] How to specify the library reading path in Python
Automatically convert to py file when ui file is updated in PySide
How to get a stacktrace in python
How to specify non-check target in Flake8
How to put a line number at the beginning of a CSV file
I made a program to check the size of a file in Python
How to sort by specifying a column in the Python Numpy array.
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
How to use the C library in Python
How to read a CSV file with Python 2/3
How to disguise a ZIP file as a PNG file
How to specify the launch browser for JupyterLab 3.0.0
How to mention a user group in slack notification, how to check the id of the user group
How to specify the NIC to scan with amazon-dash
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to drop Google Docs in one folder in a .txt file with python
How to count the number of elements in Django and output to a template
How to implement a gradient picker in Houdini
How to specify TLS version in python requests
How to save the feature point information of an image in a file and use it for matching
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to read a serial number file in a loop, process it, and graph it
Save the object to a file with pickle
[Python] How to draw a histogram in Matplotlib
Don't forget to close the file just because it's in a temporary folder
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to hide user input in PySimple GUI
Open a file dialog with a python GUI (tkinter.filedialog)
How to Mock a Public function in Pytest
How to get a list of files in the same directory with python
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
How to unit test a function containing the current time using freezegun in python