forked from qt-creator/qt-creator
Doc: Turn Qt for Python wizard docs into tutorials
Remove the Python instructions from Creating Projects and turn them into Qt Quick and Qt Widgets tutorials. Turn the Developing Qt for Python Applications topic into a How-to. Change-Id: Ia3b547fbefd5f8e6b67d673b9724cd6e3f0b4424 Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -3,27 +3,27 @@
|
||||
|
||||
/*!
|
||||
\page creator-python-development.html
|
||||
\previouspage creator-copilot.html
|
||||
\nextpage creator-mime-types.html
|
||||
\previouspage creator-how-tos.html
|
||||
|
||||
\title Developing Qt for Python Applications
|
||||
\ingroup creator-how-to-projects
|
||||
|
||||
\l {https://doc.qt.io/qtforpython/index.html}{Qt for Python} enables you
|
||||
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},
|
||||
and \l {Qt Widgets}.
|
||||
\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}.
|
||||
|
||||
The following sections describe using \QC for developing with Qt for Python:
|
||||
|
||||
\list
|
||||
\li \l{Creating Widget-Based Qt for Python Applications}
|
||||
{Creating Qt for Python Applications}
|
||||
\li \l{Setting Up PySide6}
|
||||
\li \l{Selecting the Python Interpreter}
|
||||
\li \l{Creating a Virtual Environment}
|
||||
\li \l{Using Python Interactive Shell}
|
||||
\li \l{Set up PySide6}
|
||||
\li \l{Create Qt for Python applications}
|
||||
\li \l{Select the Python interpreter}
|
||||
\li \l{Create a virtual environment}
|
||||
\li \l{Use Python interactive shell}
|
||||
\li \l{Python Language Server}
|
||||
\li \l{Running Python Projects}
|
||||
\li \l{Running Python projects}
|
||||
\li \l{Specifying Run Settings for Python Projects}
|
||||
\li \l{PDB}
|
||||
\li \l{Launching the Debugger}
|
||||
@@ -33,7 +33,7 @@
|
||||
limitations, see \l {https://doc.qt.io/qtforpython/index.html}
|
||||
{Qt for Python}.
|
||||
|
||||
\section1 Setting Up PySide6
|
||||
\section1 Set up PySide6
|
||||
|
||||
If you have not installed the required version of PySide6, \QC prompts you to
|
||||
do so when you open a .py file.
|
||||
@@ -45,12 +45,38 @@
|
||||
and annotations. Select \uicontrol Install to install PySide6 and the
|
||||
language server.
|
||||
|
||||
\section1 Selecting the Python Interpreter
|
||||
\section1 Create Qt for Python applications
|
||||
|
||||
You can use wizards to create Qt for Python application projects. The wizards
|
||||
generate a project file, \c {.pyproject}, that lists the files in the Python
|
||||
project. They also generate a \c {.py} file that has some boilerplate code.
|
||||
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.
|
||||
|
||||
\section1 Select the Python interpreter
|
||||
|
||||
You select the initial Python interpreter when you use the Qt for Python
|
||||
Application wizard templates to create Python projects.
|
||||
|
||||
\image qtcreator-python-wizard-define-python-interpreter.webp {Define Python Interpreter wizard page}
|
||||
\image qtcreator-new-qt-for-python-app-widgets-project-details.webp {Define Project Details dialog}
|
||||
|
||||
You can see the current Python interpreter on the \uicontrol Edit mode
|
||||
toolbar.
|
||||
@@ -67,30 +93,33 @@
|
||||
Or, select \uicontrol Edit > \uicontrol Preferences > \uicontrol Python >
|
||||
\uicontrol Interpreters.
|
||||
|
||||
\image qtcreator-python-interpreters.png {Python Interpreters in Preferences}
|
||||
\image qtcreator-python-interpreters.webp {Python Interpreters in Preferences}
|
||||
|
||||
You can add and remove interpreters and clean up references to interpreters
|
||||
that you uninstalled, but that still appear in the list. In addition, you
|
||||
can set the interpreter to use by default.
|
||||
|
||||
\section1 Creating a Virtual Environment
|
||||
\section1 Create a virtual environment
|
||||
|
||||
To create a virtual environment (\c venv) when you use the Qt for
|
||||
Python Application wizard templates to create Python projects, select
|
||||
the \uicontrol {Create new virtual environment} check box on the
|
||||
\uicontrol {Define Python Interpreter} wizard page. Specify the
|
||||
directory where to create the environment in
|
||||
To use a clean \l{https://docs.python.org/3/library/venv.html}{Python}
|
||||
virtual environment (\c venv) that is independent of your global Python
|
||||
installation for a Qt for Python project, select the
|
||||
\uicontrol {Create new virtual environment} check box in the project wizard.
|
||||
Set the directory where to create the environment in
|
||||
\uicontrol {Path to virtual environment}.
|
||||
|
||||
\section1 Using Python Interactive Shell
|
||||
\section1 Use Python interactive shell
|
||||
|
||||
You can write Python code in the Edit mode. Select \uicontrol REPL on the
|
||||
toolbar to start the \l{https://pythonprogramminglanguage.com/repl/}
|
||||
{Python interactive shell} in the \l Terminal pane.
|
||||
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.
|
||||
|
||||
\image qtcreator-terminal-python.webp {Python shell on the taskbar}
|
||||
\image qtcreator-terminal-python.webp {Python shell in the Terminal view}
|
||||
|
||||
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 *}.
|
||||
|
||||
\sa {Creating a Qt for Python Application with Qt Widgets},
|
||||
{Creating a Qt for Python Application with Qt Quick}
|
||||
*/
|
||||
|
||||
@@ -1,175 +0,0 @@
|
||||
// Copyright (C) 2023 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
//! [python project wizards]
|
||||
|
||||
\section2 Creating Widget-Based Qt for Python Applications
|
||||
|
||||
The Qt for Python Application wizards generate a \c {.pyproject} file that
|
||||
lists the files in the \l{Developing Qt for Python Applications}{Python}
|
||||
project and a \c {.py} file that has some boilerplate code. 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.
|
||||
|
||||
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.
|
||||
|
||||
The \uicontrol {Window UI} wizard enables you to
|
||||
create a Python project that has the source file for a class. Specify
|
||||
the PySide version, class name, base class, and source file for the
|
||||
class.
|
||||
|
||||
\image qtcreator-python-wizard-app-window.png {Define Class wizard page}
|
||||
|
||||
The wizard adds the imports to the source file for
|
||||
access to the QApplication, the base class you selected in the Qt
|
||||
Widgets module, and Qt UI tools:
|
||||
|
||||
\badcode
|
||||
import sys
|
||||
|
||||
from PySide6.QtWidgets import QApplication, QWidget
|
||||
\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
|
||||
\endcode
|
||||
|
||||
The wizard also adds a main class with the specified name that
|
||||
inherits from the specified base class:
|
||||
|
||||
\badcode
|
||||
class Widget(QWidget):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
\endcode
|
||||
|
||||
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.
|
||||
|
||||
\badcode
|
||||
self.ui = Ui_Widget()
|
||||
self.ui.setupUi(self)
|
||||
\endcode
|
||||
|
||||
\note You can access the UI elements of the new class as member variables.
|
||||
For example, if you have a button called \e{button1}, you
|
||||
can interact with it using \c{self.ui.button1}.
|
||||
|
||||
Next, 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:
|
||||
|
||||
\badcode
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
\endcode
|
||||
|
||||
Next, the wizard instantiates the \c MainWindow class and shows it:
|
||||
|
||||
\badcode
|
||||
widget = Widget()
|
||||
widget.show()
|
||||
...
|
||||
\endcode
|
||||
|
||||
Finally, the wizard calls the \c app.exec() method to enter the Qt
|
||||
main loop and start executing the Qt code:
|
||||
|
||||
\badcode
|
||||
sys.exit(app.exec())
|
||||
\endcode
|
||||
|
||||
You can now modify the boilerplate code in the Edit mode to develop your
|
||||
Python application. Always regenerate the Python code after modifying a
|
||||
UI file.
|
||||
|
||||
Open the .ui file in the \uicontrol Design mode to create a widget-based UI
|
||||
in \QD.
|
||||
|
||||
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 {Qt Quick Based Python Applications}.
|
||||
|
||||
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]
|
||||
|
||||
|
||||
//! [python qml project wizards]
|
||||
|
||||
\section1 Qt Quick Based Python Applications
|
||||
|
||||
The \uicontrol {Qt for Python - Qt Quick Application - Empty} wizard enables
|
||||
you to create a Python project that has a main QML file. Specify the
|
||||
minimum PySide version to run the application.
|
||||
|
||||
\image qtcreator-python-wizard-qml.png {Qt for Python wizard for creating an empty Qt Quick application}
|
||||
|
||||
The wizard adds the following imports to the source file for access
|
||||
to QGuiApplication and QQmlApplicationEngine:
|
||||
|
||||
\badcode
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtGui import QGuiApplication
|
||||
from PySide6.QtQml import QQmlApplicationEngine
|
||||
\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()
|
||||
qml_file = Path(__file__).resolve().parent / "main.qml"
|
||||
engine.load(qml_file)
|
||||
\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.
|
||||
If loading succeeds, the wizard calls the \c app.exec() method to enter the
|
||||
Qt main loop and start executing the Qt code:
|
||||
|
||||
\badcode
|
||||
if not engine.rootObjects():
|
||||
sys.exit(-1)
|
||||
sys.exit(app.exec())
|
||||
\endcode
|
||||
|
||||
Open the .qml file in the \uicontrol Edit mode to design a Qt Quick UI, or
|
||||
use \l{Qt Design Studio Manual}{\QDS}.
|
||||
|
||||
//! [python qml project wizards]
|
||||
*/
|
||||
@@ -0,0 +1,173 @@
|
||||
// Copyright (C) 2023 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\page tutorial-python-application-qt-widgets.html
|
||||
\previouspage creator-tutorials.html
|
||||
\nextpage creator-project-managing.html
|
||||
|
||||
\ingroup creator-tutorials
|
||||
|
||||
\title Creating a Qt for Python Application with Qt Widgets
|
||||
|
||||
\brief How to develop a Qt widget-based application with Python.
|
||||
|
||||
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
|
||||
\e {Hello World} in several languages.
|
||||
|
||||
\image qtcreator-new-qt-for-python-app-widgets-ready.webp {A small Qt Widgets application}
|
||||
|
||||
For more examples of creating Qt for Python applications, see
|
||||
\l {https://doc.qt.io/qtforpython/tutorials/index.html}
|
||||
{Qt for Python Examples and Tutorials}.
|
||||
|
||||
\section1 Creating an Empty Window Project
|
||||
|
||||
To create a Qt for Python application that has the source file for a main
|
||||
class:
|
||||
|
||||
\list 1
|
||||
\li Select \uicontrol File > \uicontrol {New Project} >
|
||||
\uicontrol {Application (Qt for Python)} > \uicontrol {Empty Window}
|
||||
> \uicontrol Choose.
|
||||
|
||||
The \uicontrol {Project Location} dialog opens.
|
||||
\image qtcreator-new-qt-for-python-app-widgets-project-location.webp {Project Location dialog}
|
||||
\li In \uicontrol {Name}, enter the project name. For example,
|
||||
\e {hello_world}.
|
||||
\li In \uicontrol {Create in}, enter the path for the project files.
|
||||
For example, \c {C:\Qt\examples}.
|
||||
\li Select \uicontrol{Next} (on Windows and Linux) or \uicontrol Continue
|
||||
(on \macos) to open the \uicontrol {Define Class} dialog.
|
||||
\image qtcreator-new-qt-for-python-app-widgets-define-class.webp {Define Class dialog}
|
||||
\li In \uicontrol {Class name}, type \b {MyWidget} as the class
|
||||
name.
|
||||
\li In \uicontrol {Base class}, select \b {QWidget} as the base class.
|
||||
\note The \uicontrol {Source file} field is automatically updated to
|
||||
match the name of the class.
|
||||
\li In \uicontrol {Project file}, enter a name for the project file.
|
||||
\li Select \uicontrol{Next} or \uicontrol Continue to open the
|
||||
\uicontrol {Define Project Details} dialog.
|
||||
\image qtcreator-new-qt-for-python-app-widgets-project-details.webp {Define Project Details dialog}
|
||||
\li In \uicontrol {PySide version}, select the PySide version of the
|
||||
generated code.
|
||||
\li In \uicontrol {Interpreter}, select the Python interpreter to use for
|
||||
the project.
|
||||
\li Select the \uicontrol {Create new virtual environment} check box to
|
||||
use a clean \l{https://docs.python.org/3/library/venv.html}{Python}
|
||||
environment that is independent of your global Python installation.
|
||||
\li In \uicontrol {Path to virtual environment}, specify the directory
|
||||
where to create the environment.
|
||||
\li Select \uicontrol{Next} or \uicontrol Continue.
|
||||
\li Review the project settings, and select \uicontrol {Finish} (on
|
||||
Windows and Linux) or \uicontrol Done (on \macos) to create the
|
||||
project.
|
||||
\endlist
|
||||
|
||||
The wizard generates the following files:
|
||||
|
||||
\list
|
||||
\li \c {hellow_world.pyproject}, which lists the files in the Python
|
||||
project.
|
||||
\li \c {mywidget.py}, which has some boilerplate code for a class.
|
||||
\endlist
|
||||
|
||||
\section1 Adding Qt Widgets Imports
|
||||
|
||||
The wizard adds the imports to the \c 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 \c random and QtCore for randomly
|
||||
selecting the language of the displayed text and QtWidgets for adding UI
|
||||
elements:
|
||||
|
||||
\badcode
|
||||
import sys
|
||||
import random
|
||||
from PySide6.QtWidgets import QApplication, QWidget
|
||||
from PySide6 import QtCore, QtWidgets
|
||||
\endcode
|
||||
|
||||
\section1 Adding a Widgets-Based UI
|
||||
|
||||
The wizard adds a main class with the specified name that inherits from the
|
||||
specified base class:
|
||||
|
||||
\badcode
|
||||
class MyWidget(QWidget):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
...
|
||||
\endcode
|
||||
|
||||
Add button, label, and layout widgets to create UI elements:
|
||||
|
||||
\badcode
|
||||
...
|
||||
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)
|
||||
...
|
||||
\endcode
|
||||
|
||||
\section1 Adding Signals and Slots
|
||||
|
||||
Then, add a signal and a slot to implement the random function:
|
||||
|
||||
\badcode
|
||||
...
|
||||
self.button.clicked.connect(self.magic)
|
||||
|
||||
@QtCore.Slot()
|
||||
def magic(self):
|
||||
self.text.setText(random.choice(self.hello))
|
||||
\endcode
|
||||
|
||||
\section1 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:
|
||||
|
||||
\badcode
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
...
|
||||
\endcode
|
||||
|
||||
\section1 Instantiating the MainWindow Class
|
||||
|
||||
The wizard instantiates the \c MainWindow class and shows it:
|
||||
|
||||
\badcode
|
||||
...
|
||||
widget = MyWidget()
|
||||
widget.show()
|
||||
...
|
||||
\endcode
|
||||
|
||||
\section1 Executing the Qt Code
|
||||
|
||||
Finally, the wizard calls the \c app.exec() method to enter the Qt
|
||||
main loop and start executing the Qt code:
|
||||
|
||||
\badcode
|
||||
...
|
||||
sys.exit(app.exec())
|
||||
\endcode
|
||||
|
||||
\section1 Running the Application
|
||||
|
||||
Select the \inlineimage icons/run_small.png
|
||||
button to run the application.
|
||||
|
||||
\sa {Creating a Qt for Python Application with Qt Quick},
|
||||
{Develop Qt for Python Applications}
|
||||
*/
|
||||
@@ -0,0 +1,187 @@
|
||||
// Copyright (C) 2023 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\page creator-tutorial-python-application-qt-quick.html
|
||||
\previouspage creator-tutorials.html
|
||||
\nextpage creator-project-managing.html
|
||||
|
||||
\ingroup creator-tutorials
|
||||
|
||||
\title Creating a Qt for Python Application with Qt Quick
|
||||
|
||||
\brief How to develop a Qt Quick application with Python.
|
||||
|
||||
First, create a Qt for Python application project. Then, edit the boilerplate
|
||||
code to develop a small application that uses Qt Quick to display the text
|
||||
\e {Hello World} in several languages.
|
||||
|
||||
\image qtcreator-new-qt-for-python-app-qt-quick-empty-project-ready.webp {A small Qt Quick application}
|
||||
|
||||
\section1 Creating an Empty Project
|
||||
|
||||
To create a Qt for Python application that has a main QML file:
|
||||
|
||||
\list 1
|
||||
\li Select \uicontrol File > \uicontrol {New Project} >
|
||||
\uicontrol {Application (Qt for Python)} >
|
||||
\uicontrol {Qt Quick Application - Empty} > \uicontrol Choose.
|
||||
|
||||
The \uicontrol {Project Location} dialog opens.
|
||||
\image qtcreator-new-qt-for-python-app-qt-quick-empty-project-location.webp {Project Location dialog}
|
||||
\li In \uicontrol {Name}, enter the project name. For example,
|
||||
\e {hello_world_quick}.
|
||||
\li In \uicontrol {Create in}, enter the path for the project files.
|
||||
For example, \c {C:\Qt\examples}.
|
||||
\li Select \uicontrol{Next} (on Windows and Linux) or \uicontrol Continue
|
||||
(on \macos) to open the \uicontrol {Define Project Details} dialog.
|
||||
\image qtcreator-new-qt-for-python-app-qt-quick-empty-project-details.webp {Define Project Details dialog}
|
||||
\li In \uicontrol {PySide version}, select the PySide version of
|
||||
the generated code.
|
||||
\li In \uicontrol {Interpreter}, select the Python interpreter to use for
|
||||
the project.
|
||||
\li Select the \uicontrol {Create new virtual environment} check box to
|
||||
use a clean \l{https://docs.python.org/3/library/venv.html}{Python}
|
||||
environment that is independent of your global Python installation.
|
||||
\li In \uicontrol {Path to virtual environment}, specify the directory
|
||||
where to create the environment.
|
||||
\li Select \uicontrol{Next} or \uicontrol Continue.
|
||||
\li Review the project settings, and select \uicontrol {Finish} (on
|
||||
Windows and Linux) or \uicontrol Done (on \macos) to create the
|
||||
project.
|
||||
\endlist
|
||||
|
||||
The wizard generates the following files:
|
||||
|
||||
\list
|
||||
\li \c {hello_world_quick.pyproject}, which lists the files in the Python
|
||||
project.
|
||||
\li \c {main.py}, which has some boilerplate code.
|
||||
\li \c {main.qml}, which imports Qt Quick controls.
|
||||
\endlist
|
||||
|
||||
\section1 Adding Qt Quick Imports
|
||||
|
||||
The wizard adds the following imports to the \c {main.py} source file for
|
||||
access to QGuiApplication and QQmlApplicationEngine:
|
||||
|
||||
\badcode
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtGui import QGuiApplication
|
||||
from PySide6.QtQml import QQmlApplicationEngine
|
||||
\endcode
|
||||
|
||||
\section1 Adding a Main Function
|
||||
|
||||
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
|
||||
|
||||
\section1 Loading the QML File
|
||||
|
||||
The following lines in the main class create a QQmlApplicationEngine
|
||||
instance and load the generated QML file to the engine object:
|
||||
|
||||
\badcode
|
||||
...
|
||||
engine = QQmlApplicationEngine()
|
||||
qml_file = Path(__file__).resolve().parent / "main.qml"
|
||||
engine.load(qml_file)
|
||||
...
|
||||
\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.
|
||||
If loading succeeds, the wizard calls the \c app.exec() method to enter the
|
||||
Qt main loop and start executing the Qt code:
|
||||
|
||||
\badcode
|
||||
...
|
||||
if not engine.rootObjects():
|
||||
sys.exit(-1)
|
||||
sys.exit(app.exec())
|
||||
...
|
||||
\endcode
|
||||
|
||||
\section1 Designing the UI
|
||||
|
||||
Open the \c {main.qml} file in the \uicontrol Edit mode to design a
|
||||
Qt Quick UI.
|
||||
|
||||
\section2 Adding Imports
|
||||
|
||||
Add imports for Qt Quick Controls and Layouts:
|
||||
|
||||
\badcode
|
||||
import QtQuick
|
||||
import QtQuick.Window
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Layouts
|
||||
\endcode
|
||||
|
||||
\section2 Adding Properties and Functions
|
||||
|
||||
The wizard adds a main window:
|
||||
|
||||
\badcode
|
||||
Window {
|
||||
width: 640
|
||||
height: 480
|
||||
visible: true
|
||||
title: qsTr("Hello World")
|
||||
}
|
||||
\endcode
|
||||
|
||||
Add a property and function to randomly select the language of the displayed
|
||||
text:
|
||||
|
||||
\badcode
|
||||
...
|
||||
readonly property list<string> texts: ["Hallo Welt", "Hei maailma",
|
||||
"Hola Mundo", "Привет мир"]
|
||||
|
||||
function setText() {
|
||||
var i = Math.round(Math.random() * 3)
|
||||
text.text = texts[i]
|
||||
}
|
||||
\endcode
|
||||
|
||||
\section2 Adding Qt Quick Controls
|
||||
|
||||
Add \l {Text} and \l {Button} QML types within a \l {ColumnLayout} type to
|
||||
design the UI:
|
||||
|
||||
\badcode
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
|
||||
Text {
|
||||
id: text
|
||||
text: "Hello World"
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
}
|
||||
Button {
|
||||
text: "Click me"
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
onClicked: setText()
|
||||
}
|
||||
}
|
||||
\endcode
|
||||
|
||||
You can also use \l{Qt Design Studio Manual}{\QDS} to design Qt Quick UIs.
|
||||
|
||||
\section1 Running the Application
|
||||
|
||||
Select the \inlineimage icons/run_small.png
|
||||
button to run the application.
|
||||
|
||||
\sa {Creating a Qt for Python Application with Qt Widgets},
|
||||
{Develop Qt for Python Applications}
|
||||
*/
|
||||
Reference in New Issue
Block a user