Creating a Qt for Python Application with Qt Widgets
First, create a Qt for Python application project. Then, edit the boilerplate code to develop a small application that uses Qt widgets to display the text Hello World in several languages.
For more examples of creating Qt for Python applications, see Qt for Python Examples and Tutorials.
To create a Qt for Python application that has the source file for a main class:
- Select File > New Project > Application (Qt for Python) > Empty Window > Choose.
The Project Location dialog opens.
- In Name, enter the project name. For example, hello_world.
- In Create in, enter the path for the project files. For example,
- Select Next (on Windows and Linux) or Continue (on macOS) to open the Define Class dialog.
- In Class name, type MyWidget as the class name.
- In Base class, select QWidget as the base class.
Note: The Source file field is automatically updated to match the name of the class.
- In Project file, enter a name for the project file.
- Select Next or Continue to open the Define Project Details dialog.
- In PySide version, select the PySide version of the generated code.
- In Interpreter, select the Python interpreter to use for the project.
- Select the Create new virtual environment check box to use a clean Python environment that is independent of your global Python installation.
- In Path to virtual environment, specify the directory where to create the environment.
- Select Next or Continue.
- Review the project settings, and select Finish (on Windows and Linux) or Done (on macOS) to create the project.
The wizard generates the following files:
hellow_world.pyproject, which lists the files in the Python project.
mywidget.py, which has some boilerplate code for a class.
The wizard adds the imports to the
mywidget.py source file for access to the QApplication and the base class you selected in the Qt Widgets module, QWidget. In addition, you need to import
random and QtCore for randomly selecting the language of the displayed text and QtWidgets for adding UI elements:
import sys import random from PySide6.QtWidgets import QApplication, QWidget from PySide6 import QtCore, QtWidgets
The wizard adds a main class with the specified name that inherits from the specified base class:
class MyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) ...
Add button, label, and layout widgets to create UI elements:
... self.hello = ["Hallo Welt", "Hei maailma", "Hola Mundo", "Привет мир"] self.button = QtWidgets.QPushButton("Click me!") self.text = QtWidgets.QLabel("Hello World", alignment=QtCore.Qt.AlignCenter) self.layout = QtWidgets.QVBoxLayout(self) self.layout.addWidget(self.text) self.layout.addWidget(self.button) ...
Then, add a signal and a slot to implement the random function:
... self.button.clicked.connect(self.magic) @QtCore.Slot() def magic(self): self.text.setText(random.choice(self.hello))
The wizard adds a main function, where it creates a QApplication instance. As Qt can receive arguments from the command line, you can pass any arguments to the QApplication object. Usually, you do not need to pass any arguments, and you can use the following approach:
if __name__ == "__main__": app = QApplication(sys.argv) ...
The wizard instantiates the
MainWindow class and shows it:
... widget = MyWidget() widget.show() ...
Finally, the wizard calls the
app.exec() method to enter the Qt main loop and start executing the Qt code:
Select the button to run the application.
© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.