diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2024-08-08 09:00:00 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2024-08-08 14:06:06 +0200 |
| commit | 89e70719d9873c0e552d86533ee8f39c70270b7e (patch) | |
| tree | 4cbea3395faa37018248af6b8419c488d1eee405 /sources/pyside6/doc/tutorials/qmlintegration | |
| parent | 0e9652d5542fccafdf8832ef5942b15040a1d8f2 (diff) | |
Documentation: Rewrite 'QML Integration' to use loadFromModule()
As a drive-by, change the resource file name to the naming convention
used by pyside6-project and fix some snippet lines.
Pick-to: 6.7
Task-number: PYSIDE-2833
Change-Id: Id73b0584e45a58f20eb1a53892943119fe4db6a4
Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/pyside6/doc/tutorials/qmlintegration')
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlintegration/Main/Main.qml (renamed from sources/pyside6/doc/tutorials/qmlintegration/view.qml) | 0 | ||||
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlintegration/Main/qmldir | 2 | ||||
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlintegration/main.py | 11 | ||||
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlintegration/qmlintegration.rst | 32 |
4 files changed, 26 insertions, 19 deletions
diff --git a/sources/pyside6/doc/tutorials/qmlintegration/view.qml b/sources/pyside6/doc/tutorials/qmlintegration/Main/Main.qml index 635603fac..635603fac 100644 --- a/sources/pyside6/doc/tutorials/qmlintegration/view.qml +++ b/sources/pyside6/doc/tutorials/qmlintegration/Main/Main.qml diff --git a/sources/pyside6/doc/tutorials/qmlintegration/Main/qmldir b/sources/pyside6/doc/tutorials/qmlintegration/Main/qmldir new file mode 100644 index 000000000..8ad738e16 --- /dev/null +++ b/sources/pyside6/doc/tutorials/qmlintegration/Main/qmldir @@ -0,0 +1,2 @@ +module Main +Main 254.0 Main.qml diff --git a/sources/pyside6/doc/tutorials/qmlintegration/main.py b/sources/pyside6/doc/tutorials/qmlintegration/main.py index b6c35c562..574e314de 100644 --- a/sources/pyside6/doc/tutorials/qmlintegration/main.py +++ b/sources/pyside6/doc/tutorials/qmlintegration/main.py @@ -3,14 +3,13 @@ from __future__ import annotations import sys -from pathlib import Path from PySide6.QtCore import QObject, Slot from PySide6.QtGui import QGuiApplication from PySide6.QtQml import QQmlApplicationEngine, QmlElement from PySide6.QtQuickControls2 import QQuickStyle -import style_rc # noqa F401 +import rc_style # noqa F401 # To be used on the @QmlElement decorator # (QML_IMPORT_MINOR_VERSION is optional) @@ -53,11 +52,9 @@ if __name__ == '__main__': app = QGuiApplication(sys.argv) QQuickStyle.setStyle("Material") engine = QQmlApplicationEngine() - - # Get the path of the current directory, and then add the name - # of the QML file, to load it. - qml_file = Path(__file__).parent / 'view.qml' - engine.load(qml_file) + # Add the current directory to the import paths and load the main module. + engine.addImportPath(sys.path[0]) + engine.loadFromModule("Main", "Main") if not engine.rootObjects(): sys.exit(-1) diff --git a/sources/pyside6/doc/tutorials/qmlintegration/qmlintegration.rst b/sources/pyside6/doc/tutorials/qmlintegration/qmlintegration.rst index 925684e26..3d127529c 100644 --- a/sources/pyside6/doc/tutorials/qmlintegration/qmlintegration.rst +++ b/sources/pyside6/doc/tutorials/qmlintegration/qmlintegration.rst @@ -29,8 +29,8 @@ application and PySide6 integration: .. literalinclude:: main.py :linenos: - :lines: 52-67 - :emphasize-lines: 4,9 + :lines: 51-64 + :emphasize-lines: 6,7 Notice that we only need a :code:`QQmlApplicationEngine` to :code:`load` the QML file. @@ -41,7 +41,7 @@ application and PySide6 integration: .. literalinclude:: main.py :linenos: :lines: 14-49 - :emphasize-lines: 4,5,8 + :emphasize-lines: 3,4,7 Notice that the registration happens thanks to the :code:`QmlElement` decorator, that underneath uses the reference to the :code:`Bridge` @@ -61,7 +61,7 @@ application and PySide6 integration: This :code:`id` will help you to get a reference to the element that was registered from Python. - .. literalinclude:: view.qml + .. literalinclude:: Main/Main.qml :linenos: :lines: 45-55 :emphasize-lines: 6-8 @@ -75,13 +75,20 @@ application and PySide6 integration: will return *False*, that is how we make sure only one is being applied to the text. +#. Put the file into into a directory named :code:`Main` along + with a file named :code:`qmldir` to describe a basic QML module: + + .. code-block:: text + + module Main + Main 254.0 Main.qml + #. Each slot verifies if the selected option contains the text associated to the property: .. literalinclude:: main.py :linenos: - :lines: 42-47 - :emphasize-lines: 4,6 + :lines: 42-44 Returning *True* or *False* allows you to activate and deactivate the properties of the QML UI elements. @@ -91,7 +98,7 @@ application and PySide6 integration: .. literalinclude:: main.py :linenos: - :lines: 34-39 + :lines: 33-36 #. Now, for changing the look of our application, you have two options: @@ -109,13 +116,13 @@ application and PySide6 integration: .. literalinclude:: style.qrc :linenos: - Generate the *rc* file running, ``pyside6-rcc style.qrc -o style_rc.py`` + Generate the *rc* file running, ``pyside6-rcc style.qrc -o rc_style.py`` And finally import it from your ``main.py`` script. .. literalinclude:: main.py :linenos: - :lines: 4-12 - :emphasize-lines: 9 + :lines: 5-12 + :emphasize-lines: 8 You can read more about this configuration file `here <https://doc.qt.io/qt-5/qtquickcontrols2-configuration.html>`_. @@ -124,5 +131,6 @@ application and PySide6 integration: .. image:: textproperties_material.png -You can :download:`view.qml <view.qml>` and -:download:`main.py <main.py>` to try this example. +You can download :download:`Main.qml <Main/Main.qml>`, +:download:`qmldir <Main/qmldir>` and :download:`main.py <main.py>` +to try this example. |
