2019-03-21 13:31:32 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2021-11-11 16:06:20 +01:00
|
|
|
** Copyright (C) 2021 The Qt Company Ltd.
|
2019-03-21 13:31:32 +01:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of the Qt Creator documentation.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU Free Documentation License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Free
|
|
|
|
|
** Documentation License version 1.3 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file included in the packaging of
|
|
|
|
|
** this file. Please review the following information to ensure
|
|
|
|
|
** the GNU Free Documentation License version 1.3 requirements
|
|
|
|
|
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
//! [python project wizards]
|
|
|
|
|
|
2020-01-15 11:06:44 +01:00
|
|
|
\section2 Creating Widget-Based Qt for Python Applications
|
2019-03-21 13:31:32 +01:00
|
|
|
|
|
|
|
|
\l {https://doc.qt.io/qtforpython/index.html}{Qt for Python} enables you
|
2021-11-11 16:06:20 +01:00
|
|
|
to use Qt 6 API in Python applications. You can use the PySide6 modules
|
|
|
|
|
to gain access to individual Qt modules, such as \l {Qt Core}, \l {Qt GUI},
|
2019-03-21 13:31:32 +01:00
|
|
|
and \l {Qt Widgets}.
|
|
|
|
|
|
2022-06-30 15:30:55 +02:00
|
|
|
If you have not installed PySide6, \QC prompts you to install it after
|
|
|
|
|
the project is created. Further, it prompts you to install the
|
|
|
|
|
\l {Python Language Server}{Python language server} that provides services
|
|
|
|
|
such as code completion and annotations. Select \uicontrol Install to install
|
|
|
|
|
PySide6 and the language server.
|
|
|
|
|
|
|
|
|
|
To view and manage the available Python interpreters, select \uicontrol Edit
|
|
|
|
|
> \uicontrol Preferences > \uicontrol Python > \uicontrol Interpreters.
|
|
|
|
|
|
|
|
|
|
\image qtcreator-python-interpreters.png "Python Interpreters in Preferences"
|
|
|
|
|
|
|
|
|
|
You can add and remove interpreters and clean up references to interpreters
|
|
|
|
|
that have been uninstalled, but still appear in the list. In addition, you
|
|
|
|
|
can set the interpreter to use by default.
|
|
|
|
|
|
2019-03-21 13:31:32 +01:00
|
|
|
The Qt for Python Application wizards generate a \c {.pyproject} file that
|
|
|
|
|
lists the files in the Python project and a \c {.py} file that contains
|
2020-01-15 11:06:44 +01:00
|
|
|
some boilerplate code. In addition, the widget based UI wizard creates a
|
|
|
|
|
\c {.ui} file that contains a \QD form, and the Qt Quick Application wizard
|
|
|
|
|
creates a \c {.qml} file that contains Qt Quick controls.
|
2019-03-21 13:31:32 +01:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2022-01-17 16:29:37 +01:00
|
|
|
The \uicontrol {Window UI} wizard enables you to
|
2020-01-15 11:06:44 +01:00
|
|
|
create a Python project that contains the source file for a class. Specify
|
2021-11-11 16:06:20 +01:00
|
|
|
the PySide version, class name, base class, and and source file for the
|
|
|
|
|
class.
|
2019-07-03 13:25:59 +02:00
|
|
|
|
2020-06-25 10:48:00 +02:00
|
|
|
\image qtcreator-python-wizard-app-window.png "Qt for Python wizard for creating a widget-based UI"
|
2019-07-03 13:25:59 +02:00
|
|
|
|
2020-01-15 11:06:44 +01:00
|
|
|
The wizard adds the imports to the source file to provide
|
|
|
|
|
access to the QApplication, the base class you selected in the Qt
|
|
|
|
|
Widgets module, and Qt UI tools:
|
2019-03-21 13:31:32 +01:00
|
|
|
|
|
|
|
|
\badcode
|
2021-11-11 16:06:20 +01:00
|
|
|
import sys
|
2022-01-17 16:29:37 +01:00
|
|
|
from pathlib import Path
|
2020-01-15 11:06:44 +01:00
|
|
|
|
2021-11-11 16:06:20 +01:00
|
|
|
from PySide6.QtWidgets import QApplication, QWidget
|
2022-01-17 16:29:37 +01:00
|
|
|
\endcode
|
|
|
|
|
|
|
|
|
|
\note It is important that you first create the Python code
|
|
|
|
|
from your UI form. In PySide6, you can do this by executing
|
|
|
|
|
\c{pyside6-uic form.ui -o ui_form.py} on a terminal. This
|
|
|
|
|
enables you to import the class that represents your UI
|
|
|
|
|
from that Python file.
|
|
|
|
|
|
|
|
|
|
Once you generate the Python code from the UI file,
|
|
|
|
|
you can import the class:
|
|
|
|
|
|
|
|
|
|
\badcode
|
|
|
|
|
from ui_form import Ui_Widget
|
2019-03-21 13:31:32 +01:00
|
|
|
\endcode
|
|
|
|
|
|
2020-01-15 11:06:44 +01:00
|
|
|
The wizard also adds a main class with the specified name that
|
2019-07-03 13:25:59 +02:00
|
|
|
inherits from the specified base class:
|
2019-03-21 13:31:32 +01:00
|
|
|
|
|
|
|
|
\badcode
|
2021-11-11 16:06:20 +01:00
|
|
|
class Widget(QWidget):
|
2022-01-17 16:29:37 +01:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
super().__init__(parent)
|
2020-01-15 11:06:44 +01:00
|
|
|
\endcode
|
|
|
|
|
|
2022-01-17 16:29:37 +01:00
|
|
|
The following lines in the main class instantiate the generated Python class from
|
|
|
|
|
your UI file, and set up the interface for the current class.
|
2020-01-15 11:06:44 +01:00
|
|
|
|
|
|
|
|
\badcode
|
2022-01-17 16:29:37 +01:00
|
|
|
self.ui = Ui_Widget()
|
|
|
|
|
self.ui.setupUi(self)
|
2019-03-21 13:31:32 +01:00
|
|
|
\endcode
|
|
|
|
|
|
2022-01-17 16:29:37 +01:00
|
|
|
\note UI elements of the new class can be accessed as member variables.
|
|
|
|
|
For example, if you have a button called \e{button1}, you
|
|
|
|
|
can interact with it using \c{self.ui.button1}.
|
|
|
|
|
|
2020-01-15 11:06:44 +01:00
|
|
|
Next, the wizard adds a main function, where it creates a
|
2019-03-21 13:31:32 +01:00
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
\badcode
|
|
|
|
|
if __name__ == "__main__":
|
2022-01-17 16:29:37 +01:00
|
|
|
app = QApplication(sys.argv)
|
2019-03-21 13:31:32 +01:00
|
|
|
\endcode
|
|
|
|
|
|
2020-01-15 11:06:44 +01:00
|
|
|
Next, the wizard instantiates the \c MainWindow class and shows it:
|
2019-03-21 13:31:32 +01:00
|
|
|
|
|
|
|
|
\badcode
|
2021-11-11 16:06:20 +01:00
|
|
|
widget = Widget()
|
|
|
|
|
widget.show()
|
2019-03-21 13:31:32 +01:00
|
|
|
...
|
|
|
|
|
\endcode
|
|
|
|
|
|
2022-01-17 16:29:37 +01:00
|
|
|
Finally, the wizard calls the \c app.exec() method to enter the Qt
|
2019-03-21 13:31:32 +01:00
|
|
|
main loop and start executing the Qt code:
|
|
|
|
|
|
|
|
|
|
\badcode
|
2022-01-17 16:29:37 +01:00
|
|
|
sys.exit(app.exec())
|
2019-03-21 13:31:32 +01:00
|
|
|
\endcode
|
|
|
|
|
|
2020-06-25 10:48:00 +02:00
|
|
|
You can now modify the boilerplate code in the Edit mode to develop your
|
|
|
|
|
Python application. Select \uicontrol REPL on the toolbar to start the
|
|
|
|
|
\l{https://pythonprogramminglanguage.com/repl/}{Python interactive shell}.
|
|
|
|
|
To start the shell and import the current file as a module, select
|
|
|
|
|
select \uicontrol {REPL Import File}. To also import all functions from
|
|
|
|
|
the file, select \uicontrol {REPL Import *}.
|
|
|
|
|
|
2022-01-17 16:29:37 +01:00
|
|
|
Always regenerate the Python code after modifying a UI file.
|
|
|
|
|
|
2021-10-07 12:14:17 +02:00
|
|
|
Open the .ui file in the \uicontrol Design mode to create a widget-based UI
|
|
|
|
|
in \QD.
|
2020-01-15 11:06:44 +01:00
|
|
|
|
|
|
|
|
The \uicontrol Window wizard adds similar code to the source file, without
|
|
|
|
|
the UI bits.
|
|
|
|
|
|
|
|
|
|
The \uicontrol Empty wizard adds similar code to the source file, but it
|
|
|
|
|
does not add any classes, so you need to add and instantiate them yourself.
|
|
|
|
|
|
|
|
|
|
For more information about the
|
|
|
|
|
\uicontrol {Qt for Python - Qt Quick Application - Empty} wizard, see
|
|
|
|
|
\l {Creating Qt Quick Based Python Applications}.
|
2019-03-21 13:31:32 +01:00
|
|
|
|
|
|
|
|
For examples of creating Qt for Python applications, see
|
|
|
|
|
\l {https://doc.qt.io/qtforpython/tutorials/index.html}
|
|
|
|
|
{Qt for Python Examples and Tutorials}.
|
|
|
|
|
|
|
|
|
|
//! [python project wizards]
|
2020-01-15 11:06:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//! [python qml project wizards]
|
|
|
|
|
|
|
|
|
|
\section1 Creating Qt Quick Based Python Applications
|
|
|
|
|
|
|
|
|
|
The \uicontrol {Qt for Python - Qt Quick Application - Empty} wizard enables
|
|
|
|
|
you to create a Python project that contains a main QML file. Specify the
|
|
|
|
|
minimum PySide version to run the application.
|
|
|
|
|
|
2020-06-25 10:48:00 +02:00
|
|
|
\image qtcreator-python-wizard-qml.png "Qt for Python wizard for creating an empty Qt Quick application"
|
2020-01-15 11:06:44 +01:00
|
|
|
|
|
|
|
|
The wizard adds the following imports to the source file to provide access
|
|
|
|
|
to QGuiApplication and QQmlApplicationEngine:
|
|
|
|
|
|
|
|
|
|
\badcode
|
|
|
|
|
import os
|
2021-11-11 16:06:20 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
import sys
|
2020-01-15 11:06:44 +01:00
|
|
|
|
2021-11-11 16:06:20 +01:00
|
|
|
from PySide6.QtGui import QGuiApplication
|
|
|
|
|
from PySide6.QtQml import QQmlApplicationEngine
|
2020-01-15 11:06:44 +01:00
|
|
|
\endcode
|
|
|
|
|
|
|
|
|
|
The wizard also adds a main function, where it creates a QGuiApplication
|
|
|
|
|
instance and passes system arguments to the QGuiApplication object:
|
|
|
|
|
|
|
|
|
|
\badcode
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
app = QGuiApplication(sys.argv)
|
|
|
|
|
...
|
|
|
|
|
\endcode
|
|
|
|
|
|
|
|
|
|
The following lines in the main class create a QQmlApplicationEngine
|
|
|
|
|
instance and load the generated QML file to the engine object:
|
|
|
|
|
|
|
|
|
|
\badcode
|
|
|
|
|
engine = QQmlApplicationEngine()
|
2021-11-11 16:06:20 +01:00
|
|
|
engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml"))
|
2020-01-15 11:06:44 +01:00
|
|
|
\endcode
|
|
|
|
|
|
|
|
|
|
Finally, the wizard adds code that checks whether the file was successfully
|
|
|
|
|
loaded. If loading the file fails, the application exits with an error code.
|
2022-01-17 16:29:37 +01:00
|
|
|
If loading succeeds, the wizard calls the \c app.exec() method to enter the
|
2020-01-15 11:06:44 +01:00
|
|
|
Qt main loop and start executing the Qt code:
|
|
|
|
|
|
|
|
|
|
\badcode
|
|
|
|
|
if not engine.rootObjects():
|
|
|
|
|
sys.exit(-1)
|
2022-01-17 16:29:37 +01:00
|
|
|
sys.exit(app.exec())
|
2020-01-15 11:06:44 +01:00
|
|
|
\endcode
|
|
|
|
|
|
2021-10-07 12:14:17 +02:00
|
|
|
Open the .qml file in the \uicontrol Edit mode to design a Qt Quick UI, or
|
2021-11-11 16:06:20 +01:00
|
|
|
use \l{Qt Design Studio Manual}{\QDS}.
|
2020-01-15 11:06:44 +01:00
|
|
|
|
|
|
|
|
//! [python qml project wizards]
|
2019-03-21 13:31:32 +01:00
|
|
|
*/
|