doc_qt_io_qtforpython_6_tutorials_basictutorial_uifiles_html
doc_qt_io_qtforpython_6_tutorials_basictutorial_uifiles_html
The designs are stored in .ui files, which is an XML-based format. It will be converted to Python or C++ code
populating a widget instance at project build time by the pyside6-uic tool.
To create a new Qt Design Form in Qt Creator, choose File/New File Or Project and “Main Window” for
template. Save it as mainwindow.ui . Add a QPushButton to the center of the centralwidget.
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>110</x>
<y>80</y>
<width>201</width>
<height>81</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Now we are ready to decide how to use the UI file from Python.
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF
The standard way to interact with a UI file is to generate a Python class from it. This is possible thanks to the
pyside6-uic tool. To use this tool, you need to run the following command on a console:
We redirect all the output of the command to a file called ui_mainwindow.py , which will be imported directly:
Now to use it, we should create a personalized class for our widget to setup this generated design.
import sys
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtCore import QFile
from ui_mainwindow import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
What is inside the if statement is already known from the previous examples, and our new basic class
contains only two new lines that are in charge of loading the generated python class from the UI file:
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
Note
You must run pyside6-uic again every time you make changes to the UI file.
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF
The QUiLoader lets us load the ui file dynamically and use it right away:
ui_file = QFile("mainwindow.ui")
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
window = loader.load(ui_file)
window.show()
# File: main.py
import sys
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QFile, QIODevice
if __name__ == "__main__":
app = QApplication(sys.argv)
ui_file_name = "mainwindow.ui"
ui_file = QFile(ui_file_name)
if not ui_file.open(QIODevice.ReadOnly):
print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
sys.exit(-1)
loader = QUiLoader()
window = loader.load(ui_file)
ui_file.close()
if not window:
print(loader.errorString())
sys.exit(-1)
window.show()
sys.exit(app.exec())
python main.py
Note
QUiLoader uses connect() calls taking the function signatures as string arguments for signal/slot connections. It is thus unable to
handle Python types like str or list from custom widgets written in Python since these types are internally mapped to different
C++ types.
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF
Normally, this requires implementing the widget as a plugin to Qt Widgets Designer written in C++
implementing its QDesignerCustomWidgetInterface .
Qt for Python provides a simple interface for this which is similar to registerCustomWidget() .
The widget needs to be provided as a Python module, as shown by the WigglyWidget Example (file
wigglywidget.py ) or the Task Menu Extension Example (file tictactoe.py ).
Registering this with Qt Widgets Designer is done by providing a registration script named register*.py and
pointing the path-type environment variable PYSIDE_DESIGNER_PLUGINS to the directory.
# File: registerwigglywidget.py
from wigglywidget import WigglyWidget
import QtDesigner
QPyDesignerCustomWidgetCollection.registerCustomWidget(WigglyWidget, module="wigglywidget",
tool_tip=TOOLTIP, xml=DOM_XML)
The function registerCustomWidget() is used to register a widget type with Qt Widgets Designer. In the
simple case, it can be used like QUiLoader.registerCustomWidget() . It takes the custom widget type and
some optional keyword arguments passing values that correspond to the getters of
QDesignerCustomWidgetInterface :
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF
When launching Qt Widgets Designer via its launcher pyside6-designer , the custom widget should be visible
in the widget box.
For advanced usage, it is also possible to pass the function an implementation of the class
QDesignerCustomWidgetInterface instead of the type to addCustomWidget() . This is shown in
taskmenuextension example, where a custom context menu is registered for the custom widget. The example
is a port of the corresponding C++ Task Menu Extension Example .
Explore our developer-friendly HTML to PDF API Printed using PDFCrowd HTML to PDF