diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2024-08-09 09:40:58 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2024-08-09 14:10:24 +0200 |
| commit | 55c082ebe8c1cd1111ad8d772c13ddebb3b75502 (patch) | |
| tree | e8143bbee75b2906cf2062792a6454b79aebfa42 /sources/pyside6/doc/tutorials/qmlapp/main.py | |
| parent | 8ff61a10d142e6f51efcc26535f460e33a1cc0ed (diff) | |
Documentation: Rewrite 'QML Application' tutorial to use loadFromModule()
Pick-to: 6.7
Task-number: PYSIDE-2833
Change-Id: I8c4e9af9fe46cdd57f3fba65f8d9200512addea0
Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Diffstat (limited to 'sources/pyside6/doc/tutorials/qmlapp/main.py')
| -rw-r--r-- | sources/pyside6/doc/tutorials/qmlapp/main.py | 22 |
1 files changed, 11 insertions, 11 deletions
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 |
