2024-02-21 16:59:07 +01:00
|
|
|
// Copyright (C) 2024 The Qt Company Ltd.
|
2023-07-31 16:26:12 +02:00
|
|
|
// 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
|
2024-02-01 16:21:27 +01:00
|
|
|
\nextpage creator-how-tos.html
|
2023-07-31 16:26:12 +02:00
|
|
|
|
|
|
|
\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}
|
|
|
|
|
2024-02-21 16:59:07 +01:00
|
|
|
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}.
|
|
|
|
|
2023-07-31 16:26:12 +02:00
|
|
|
\section1 Creating an Empty Project
|
|
|
|
|
|
|
|
To create a Qt for Python application that has a main QML file:
|
|
|
|
|
|
|
|
\list 1
|
2024-02-21 16:59:07 +01:00
|
|
|
\li Go to \uicontrol File > \uicontrol {New Project}.
|
|
|
|
\li Select \uicontrol {Application (Qt for Python)} >
|
|
|
|
\uicontrol {Qt Quick Application - Empty} > \uicontrol Choose to
|
|
|
|
open the \uicontrol {Project Location} dialog.
|
2023-07-31 16:26:12 +02:00
|
|
|
\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.
|
2024-03-20 10:27:12 +01:00
|
|
|
For example, \c {C:\Examples}.
|
2023-07-31 16:26:12 +02:00
|
|
|
\li Select \uicontrol{Next} (on Windows and Linux) or \uicontrol Continue
|
|
|
|
(on \macos) to open the \uicontrol {Define Project Details} dialog.
|
2024-02-21 16:59:07 +01:00
|
|
|
\image qtcreator-new-qt-for-python-app-project-details.webp {Define Project Details dialog}
|
2023-07-31 16:26:12 +02:00
|
|
|
\li In \uicontrol {PySide version}, select the PySide version of
|
|
|
|
the generated code.
|
2024-02-21 16:59:07 +01:00
|
|
|
\li Select \uicontrol{Next} or \uicontrol Continue to open the
|
|
|
|
\uicontrol {Kit Selection} dialog.
|
|
|
|
\image qtcreator-new-project-qt-for-python-kit-selection.webp {Selecting a kit for a Python project}
|
|
|
|
\li Select Qt for Python kits for building, deploying, and running the
|
|
|
|
project.
|
2023-07-31 16:26:12 +02:00
|
|
|
\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.
|
2024-02-21 16:59:07 +01:00
|
|
|
\li \c {reguirements.txt}, which stores the PySide version of the
|
|
|
|
generated code.
|
2023-07-31 16:26:12 +02:00
|
|
|
\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}
|
|
|
|
*/
|