diff options
Diffstat (limited to 'sources/pyside6/doc/tutorials/qmlapp/main.py')
-rw-r--r-- | sources/pyside6/doc/tutorials/qmlapp/main.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/sources/pyside6/doc/tutorials/qmlapp/main.py b/sources/pyside6/doc/tutorials/qmlapp/main.py new file mode 100644 index 000000000..8b1b25440 --- /dev/null +++ b/sources/pyside6/doc/tutorials/qmlapp/main.py @@ -0,0 +1,46 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import sys +import urllib.request +import json +from pathlib import Path + +from PySide6.QtQuick import QQuickView +from PySide6.QtCore import QStringListModel, QUrl +from PySide6.QtGui import QGuiApplication + + +if __name__ == '__main__': + + #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 + data_list = list(data.values()) + data_list.sort() + + #Set up the application window + app = QGuiApplication(sys.argv) + view = QQuickView() + view.setResizeMode(QQuickView.SizeRootObjectToView) + + #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())) + + #Show the window + if view.status() == QQuickView.Error: + sys.exit(-1) + view.show() + + #execute and cleanup + app.exec() + del view |