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.

{A small Qt Widgets application}

For more examples of creating Qt for Python applications, see Qt for Python Examples and Tutorials.

Creating an Empty Window Project

To create a Qt for Python application that has the source file for a main class:

  1. Select File > New Project > Application (Qt for Python) > Empty Window > Choose.

    The Project Location dialog opens.

    {Project Location dialog}

  2. In Name, enter the project name. For example, hello_world.
  3. In Create in, enter the path for the project files. For example, C:\Qt\examples.
  4. Select Next (on Windows and Linux) or Continue (on macOS) to open the Define Class dialog.

    {Define Class dialog}

  5. In Class name, type MyWidget as the class name.
  6. In Base class, select QWidget as the base class.

    Note: The Source file field is automatically updated to match the name of the class.

  7. In Project file, enter a name for the project file.
  8. Select Next or Continue to open the Define Project Details dialog.

    {Define Project Details dialog}

  9. In PySide version, select the PySide version of the generated code.
  10. In Interpreter, select the Python interpreter to use for the project.
  11. Select the Create new virtual environment check box to use a clean Python environment that is independent of your global Python installation.
  12. In Path to virtual environment, specify the directory where to create the environment.
  13. Select Next or Continue.
  14. 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.

Adding Qt Widgets Imports

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

Adding a Widgets-Based UI

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)
...

Adding Signals and Slots

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

Adding a Main Function

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)
    ...

Instantiating the MainWindow Class

The wizard instantiates the MainWindow class and shows it:

...
widget = MyWidget()
widget.show()
...

Executing the Qt Code

Finally, the wizard calls the app.exec() method to enter the Qt main loop and start executing the Qt code:

...
sys.exit(app.exec())

Running the Application

Select the button to run the application.

See also Creating a Qt for Python Application with Qt Quick and Develop Qt for Python Applications.

© 2024 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.