diff options
Diffstat (limited to 'sources/pyside6/doc/tutorials/qmlapp')
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlapp/Main/Main.qml (renamed from sources/pyside6/doc/tutorials/qmlapp/view.qml) | 0 | ||||
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlapp/Main/logo.png (renamed from sources/pyside6/doc/tutorials/qmlapp/logo.png) | bin | 6208 -> 6208 bytes | |||
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlapp/Main/qmldir | 2 | ||||
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlapp/main.py | 22 | ||||
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlapp/qmlapplication.rst | 31 |
5 files changed, 29 insertions, 26 deletions
diff --git a/sources/pyside6/doc/tutorials/qmlapp/view.qml b/sources/pyside6/doc/tutorials/qmlapp/Main/Main.qml index 7f9b1d777..7f9b1d777 100644 --- a/sources/pyside6/doc/tutorials/qmlapp/view.qml +++ b/sources/pyside6/doc/tutorials/qmlapp/Main/Main.qml diff --git a/sources/pyside6/doc/tutorials/qmlapp/logo.png b/sources/pyside6/doc/tutorials/qmlapp/Main/logo.png Binary files differindex 30c621c9c..30c621c9c 100644 --- a/sources/pyside6/doc/tutorials/qmlapp/logo.png +++ b/sources/pyside6/doc/tutorials/qmlapp/Main/logo.png diff --git a/sources/pyside6/doc/tutorials/qmlapp/Main/qmldir b/sources/pyside6/doc/tutorials/qmlapp/Main/qmldir new file mode 100644 index 000000000..8ad738e16 --- /dev/null +++ b/sources/pyside6/doc/tutorials/qmlapp/Main/qmldir @@ -0,0 +1,2 @@ +module Main +Main 254.0 Main.qml diff --git a/sources/pyside6/doc/tutorials/qmlapp/main.py b/sources/pyside6/doc/tutorials/qmlapp/main.py index c532e8d26..3ab440531 100644 --- a/sources/pyside6/doc/tutorials/qmlapp/main.py +++ b/sources/pyside6/doc/tutorials/qmlapp/main.py @@ -5,43 +5,43 @@ from __future__ import annotations import sys import urllib.request import json -from pathlib import Path from PySide6.QtQuick import QQuickView -from PySide6.QtCore import QStringListModel, QUrl +from PySide6.QtCore import QStringListModel from PySide6.QtGui import QGuiApplication if __name__ == '__main__': - #get our data + # get our data url = "http://country.io/names.json" response = urllib.request.urlopen(url) data = json.loads(response.read().decode('utf-8')) - #Format and sort the data + # Format and sort the data data_list = list(data.values()) data_list.sort() - #Set up the application window + # Set up the application window app = QGuiApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) - #Expose the list to the Qml code + # Expose the list to the Qml code my_model = QStringListModel() my_model.setStringList(data_list) view.setInitialProperties({"myModel": my_model}) - #Load the QML file - qml_file = Path(__file__).parent / "view.qml" - view.setSource(QUrl.fromLocalFile(qml_file.resolve())) + # Load the QML file + # Add the current directory to the import paths and load the main module. + view.engine().addImportPath(sys.path[0]) + view.loadFromModule("Main", "Main") - #Show the window + # Show the window if view.status() == QQuickView.Error: sys.exit(-1) view.show() - #execute and cleanup + # execute and cleanup app.exec() del view diff --git a/sources/pyside6/doc/tutorials/qmlapp/qmlapplication.rst b/sources/pyside6/doc/tutorials/qmlapp/qmlapplication.rst index c6d72e742..5b7e7d4e0 100644 --- a/sources/pyside6/doc/tutorials/qmlapp/qmlapplication.rst +++ b/sources/pyside6/doc/tutorials/qmlapp/qmlapplication.rst @@ -42,8 +42,9 @@ development process using *Qt Creator*: This should create a ``main.py`` and ```main.pyproject`` files for the project. -#. Download :download:`view.qml<view.qml>` and :download:`logo.png <logo.png>` - and move them to your project folder. +#. Download :download:`Main.qml<Main/Main.qml>`, :download:`qmldir<Main/qmldir>` + and :download:`logo.png <Main/logo.png>` and place them in a subdirectory + named `Main` in your project folder. This creates a basic QML module. #. Double-click on ``main.pyproject`` to open it in edit mode, and append ``view.qml`` and ``logo.png`` to the **files** list. This is how your @@ -52,7 +53,7 @@ development process using *Qt Creator*: .. code:: { - "files": ["main.py", "view.qml", "logo.png"] + "files": ["main.py", "Main/Main.qml", "Main/logo.png", "Main/qmldir"] } #. Now that you have the necessary bits for the application, import the @@ -61,8 +62,8 @@ development process using *Qt Creator*: .. literalinclude:: main.py :linenos: - :lines: 3-23 - :emphasize-lines: 7-9,14-17 + :lines: 5-23 + :emphasize-lines: 5-7,12-15 #. Now, set up the application window using :ref:`PySide6.QtGui.QGuiApplication<qguiapplication>`, which manages the application-wide @@ -70,8 +71,8 @@ development process using *Qt Creator*: .. literalinclude:: main.py :linenos: - :lines: 3-28 - :emphasize-lines: 23-25 + :lines: 5-28 + :emphasize-lines: 21-24 .. note:: Setting the resize policy is important if you want the root item to resize itself to fit the window or vice-a-versa. @@ -83,23 +84,23 @@ development process using *Qt Creator*: .. literalinclude:: main.py :linenos: - :lines: 3-33 - :emphasize-lines: 28-31 + :lines: 5-33 + :emphasize-lines: 26-29 -#. Load the ``view.qml`` to the ``QQuickView`` and call ``show()`` to +#. Load the ``Main.qml`` to the ``QQuickView`` and call ``show()`` to display the application window. .. literalinclude:: main.py :linenos: - :lines: 3-42 - :emphasize-lines: 33-40 + :lines: 5-43 + :emphasize-lines: 31-39 #. Finally, execute the application to start the event loop and clean up. .. literalinclude:: main.py :linenos: - :lines: 3- - :emphasize-lines: 42-44 + :lines: 5- + :emphasize-lines: 41-43 #. Your application is ready to be run now. Select **Projects** mode to choose the Python version to run it. @@ -128,5 +129,5 @@ this application: Related information ******************** -* `QML Reference <https://doc.qt.io/qt-5/qmlreference.html>`_ +* `QML Reference <https://doc.qt.io/qt-6/qmlreference.html>`_ * :doc:`../qmlintegration/qmlintegration` |
