2023-06-07 11:53:09 +02:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\page creator-python-development.html
|
2023-07-31 16:26:12 +02:00
|
|
|
\previouspage creator-how-tos.html
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2023-07-31 16:26:12 +02:00
|
|
|
\ingroup creator-how-to-projects
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2023-07-31 16:26:12 +02:00
|
|
|
\title Develop Qt for Python applications
|
|
|
|
|
|
|
|
With \l {https://doc.qt.io/qtforpython/index.html}{Qt for Python}, you can
|
|
|
|
use Qt 6 API in Python applications. Use the PySide6 modules to gain access
|
|
|
|
to individual Qt modules, such as \l {Qt Core}, \l {Qt GUI}, and
|
|
|
|
\l {Qt Widgets}.
|
2023-06-07 11:53:09 +02:00
|
|
|
|
|
|
|
The following sections describe using \QC for developing with Qt for Python:
|
|
|
|
|
|
|
|
\list
|
2023-07-31 16:26:12 +02:00
|
|
|
\li \l{Set up PySide6}
|
|
|
|
\li \l{Create Qt for Python applications}
|
2024-02-21 16:59:07 +01:00
|
|
|
\li \l{Select the Python version}
|
|
|
|
\li \l{Create kits for Python}
|
2023-07-31 16:26:12 +02:00
|
|
|
\li \l{Use Python interactive shell}
|
2024-01-11 13:09:36 +01:00
|
|
|
\li \l{Configure Python language servers}
|
2023-11-06 18:22:45 +01:00
|
|
|
\li \l{Run Python applications}
|
|
|
|
\li \l{Python Run Settings}
|
2024-04-19 11:35:05 +02:00
|
|
|
\li \l{PDB versions}
|
2024-06-27 13:36:26 +02:00
|
|
|
\li \l{Supported Debuggers}
|
2023-06-07 11:53:09 +02:00
|
|
|
\endlist
|
|
|
|
|
|
|
|
For more information about developing with Qt for Python, including
|
|
|
|
limitations, see \l {https://doc.qt.io/qtforpython/index.html}
|
|
|
|
{Qt for Python}.
|
|
|
|
|
2023-07-31 16:26:12 +02:00
|
|
|
\section1 Set up PySide6
|
2023-06-07 11:53:09 +02:00
|
|
|
|
|
|
|
If you have not installed the required version of PySide6, \QC prompts you to
|
|
|
|
do so when you open a .py file.
|
|
|
|
|
|
|
|
\image qtcreator-python-install.webp {Prompts to install PySide6 and Python language server}
|
|
|
|
|
2024-01-11 13:09:36 +01:00
|
|
|
Further, \QC prompts you to install the \l {Configure Python language servers}
|
2023-06-07 11:53:09 +02:00
|
|
|
{Python language server} that offers services such as code completion
|
|
|
|
and annotations. Select \uicontrol Install to install PySide6 and the
|
|
|
|
language server.
|
|
|
|
|
2023-07-31 16:26:12 +02:00
|
|
|
\section1 Create Qt for Python applications
|
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
Use wizards to create Qt for Python application projects. The wizards
|
2023-07-31 16:26:12 +02:00
|
|
|
generate a project file, \c {.pyproject}, that lists the files in the Python
|
2024-02-21 16:59:07 +01:00
|
|
|
project. They also generate a \c {.py} file that has some boilerplate code
|
|
|
|
and \c {reguirements.txt} that stores the PySide version of the generated code.
|
2023-07-31 16:26:12 +02:00
|
|
|
In addition, the widget-based UI wizard creates a \c {.ui} file that has a
|
|
|
|
\QD form, and the Qt Quick Application wizard creates a \c {.qml} file that
|
|
|
|
imports Qt Quick controls.
|
|
|
|
|
|
|
|
\note Before importing UI classes and after editing them, create the Python
|
|
|
|
code from your UI form. In PySide6, run \c{pyside6-uic form.ui -o ui_form.py}
|
|
|
|
in the \l Terminal view.
|
|
|
|
|
|
|
|
\image qtcreator-new-qt-for-python-app-window-ui-uic.webp {Creating Python code in Terminal}
|
|
|
|
|
|
|
|
The \uicontrol Window wizard adds code to the source file, without the UI
|
|
|
|
file.
|
|
|
|
|
|
|
|
The \uicontrol Empty wizard adds code to the source file, but it
|
|
|
|
does not add any classes, so you need to add and instantiate them yourself.
|
|
|
|
|
|
|
|
The \c{.pyproject} files are JSON-based configuration files that replace
|
|
|
|
the previously used \c {.pyqtc} configuration files. You can still open and
|
|
|
|
use \c {.pyqtc} files, but we recommend that you choose \c{.pyproject} files
|
|
|
|
for new projects.
|
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
\section1 Select the Python version
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2024-06-11 14:35:07 +02:00
|
|
|
The \l{Kits}{kits} you select for the project in \uicontrol Projects >
|
2024-02-21 16:59:07 +01:00
|
|
|
\uicontrol {Build & Run} set the Python version to use.
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
The \l {Edit Mode}{Edit mode} toolbar shows the current Python version.
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
\image qtcreator-python-interpreter-edit-mode.webp {Python version on the Edit mode toolbar}
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
To use another Python version, activate another kit for the project.
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
\section1 Create kits for Python
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
\QC automatically adds all Python versions it can find to the list of
|
|
|
|
Python versions in \preferences > \uicontrol Python > \uicontrol Interpreters.
|
|
|
|
It generates kits for the global Python versions that are not inside a
|
|
|
|
virtual environment.
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2023-07-31 16:26:12 +02:00
|
|
|
\image qtcreator-python-interpreters.webp {Python Interpreters in Preferences}
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
You can add and remove Python versions and clean up references to Python
|
|
|
|
versions that you uninstalled, but that still appear in the list.
|
2024-01-12 16:09:46 +01:00
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
To use the selected Python version when opening \c {.py} files that don't
|
|
|
|
belong to a project, select \uicontrol {Make Default}.
|
2024-01-12 16:09:46 +01:00
|
|
|
|
|
|
|
To use a virtual environment as a kit, select it in \uicontrol Interpreters,
|
|
|
|
and then select \uicontrol {Generate Kit}.
|
|
|
|
|
2023-07-31 16:26:12 +02:00
|
|
|
\section1 Use Python interactive shell
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2023-07-31 16:26:12 +02:00
|
|
|
You can write Python code in the \uicontrol Edit mode. Select \uicontrol REPL
|
|
|
|
on the toolbar to start the \l{https://pythonprogramminglanguage.com/repl/}
|
|
|
|
{Python interactive shell} in the \l Terminal view.
|
2023-06-07 11:53:09 +02:00
|
|
|
|
2023-07-31 16:26:12 +02:00
|
|
|
\image qtcreator-terminal-python.webp {Python shell in the Terminal view}
|
2023-06-07 11:53:09 +02:00
|
|
|
|
|
|
|
To start the shell and import the current file as a module, select
|
|
|
|
\uicontrol {REPL Import File}. To also import all functions from
|
|
|
|
the file, select \uicontrol {REPL Import *}.
|
2023-07-31 16:26:12 +02:00
|
|
|
|
2024-07-10 16:22:50 +02:00
|
|
|
\sa {Tutorial: Qt Widgets and Python},
|
|
|
|
{Tutorial: Qt Quick and Python},
|
2024-02-21 16:59:07 +01:00
|
|
|
{Activate kits for a project}
|
2023-06-07 11:53:09 +02:00
|
|
|
*/
|