Tutorial: Qt Widgets UI and Python

First, create a Qt for Python application project. Then, use the integrated Qt Widgets Designer to design a widgets-based UI.

{A small Qt Widgets application}

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

Create a window UI project

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

  1. Go to File > New Project.
  2. Select Application (Qt for Python) > Window UI > Choose to open the Project Location dialog.

    {Project Location dialog}

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

    {Define Class dialog}

  6. In Class name, select MainWindow as the class name.
  7. In Base class, select QMainWindow as the base class.

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

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

    {Define Python Interpreter dialog}

  10. In PySide version, select the PySide version of the generated code.
  11. Select Next or Continue to open the Kit Selection dialog.

    {Selecting a kit for a Python project}

  12. Select Python kits for building, deploying, and running the project. By default, this creates a virtual environment for the project inside the source directory. To use the global interpreter, select the build configuration with the same name as the Python of the kit in Details.
  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:

  • form.ui, which is the UI file for the window UI.
  • hello_world_ui.pyproject, which lists the files in the Python project.
  • mainwindow.py, which has some boilerplate code for a class.
  • requirements.txt, which stores the PySide version of the generated code. You can use this file to install the required PySide version using pip.

Install PySide6 for the project

In the Edit mode, select Install to set up PySide6 for the project.

{Prompt to install PySide6}

Design a widgets-based UI

  1. In the Edit mode, double-click the form.ui file in the Projects view to launch the integrated Qt Widgets Designer.
  2. Drag the following widgets from Widget Box to the form:

    {Adding widgets to the UI}

    Note: To easily locate the widgets, use the search box at the top of the Widget Box. For example, to find the Text Edit widget, start typing the word text.

  3. Double-click the Text Edit widget and enter the text Enter text here.
  4. Double-click the Check Box widget and enter the text Select me.
  5. Double-click the Push Button widget and enter the text Click me.
  6. Press Ctrl+S (or Cmd+S) to save your changes.

For more information about designing UIs with Qt Widgets Designer, see Qt Widgets Designer Manual.

Run the application

Select the button to run the application.

See also Tutorial: Qt Quick and Python, Tutorial: Qt Widgets and Python, and Develop Qt for Python Applications.

Copyright © The Qt Company Ltd. and other contributors. 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.