From 5a11e30d541a9c747cdc0cdb4522f93f708cae9a Mon Sep 17 00:00:00 2001 From: Daguerreo Date: Mon, 22 Nov 2021 11:12:01 +0100 Subject: [PATCH] Fixed leak and moved json to resource file in json tree view example --- examples/JsonTreeView/JsonTreeView.pro | 3 +- examples/JsonTreeView/main.cpp | 51 +++++++------------------- examples/JsonTreeView/resources.qrc | 5 +++ examples/JsonTreeView/sample.json | 22 +++++++++++ 4 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 examples/JsonTreeView/resources.qrc create mode 100644 examples/JsonTreeView/sample.json diff --git a/examples/JsonTreeView/JsonTreeView.pro b/examples/JsonTreeView/JsonTreeView.pro index 9fa645d..b78faeb 100644 --- a/examples/JsonTreeView/JsonTreeView.pro +++ b/examples/JsonTreeView/JsonTreeView.pro @@ -8,7 +8,8 @@ DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs depr RESOURCES += qml.qrc \ ../../plugin/treeview.qrc \ - qml.qrc + qml.qrc \ + resources.qrc # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = diff --git a/examples/JsonTreeView/main.cpp b/examples/JsonTreeView/main.cpp index 44ca3c4..705f230 100644 --- a/examples/JsonTreeView/main.cpp +++ b/examples/JsonTreeView/main.cpp @@ -2,17 +2,16 @@ #include #include -#include +#include #include +#include #include #include "tree_model.h" #include "json_entry.h" - void loadValue(const QJsonValue& value, TreeItem* parent, TreeModel* model) { - if(value.isObject()) { auto object = value.toObject(); for(auto it=object.begin(); it!=object.end(); ++it){ @@ -53,55 +52,33 @@ void loadValue(const QJsonValue& value, TreeItem* parent, TreeModel* model) } } -TreeModel* populateJson() +void populateModel(TreeModel& model) { - QString json = R"( - { - "firstName": "John", - "lastName": "Smith", - "age": 25, - "address": { - "streetAddress": "21 2nd Street", - "city": "New York", - "state": "NY", - "postalCode": "10021", - "owner": true - }, - "phoneNumber": [ - { - "type": "home", - "number": "212 555-1234" - }, - { - "type": "fax", - "number": "646 555-4567" - } - ] - })"; + QFile jsonFile{":/sample.json"}; + if(!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)){ + qCritical() << "error: json file cannot be open"; + return; + } QJsonParseError error; - auto doc = QJsonDocument::fromJson(json.toUtf8(), &error); - qDebug() << error.errorString(); - - auto jsonModel = new TreeModel(); + auto doc = QJsonDocument::fromJson(jsonFile.readAll(), &error); + qDebug() << "loading json file:" << error.errorString(); auto root = doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object()); - loadValue(root, jsonModel->rootItem().get(), jsonModel); - - return jsonModel; + loadValue(root, model.rootItem(), &model); } - int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); + QQmlApplicationEngine engine; - auto jsonModel = populateJson(); + auto jsonModel = new TreeModel(&engine); + populateModel(*jsonModel); - QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("jsonModel", jsonModel); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect( diff --git a/examples/JsonTreeView/resources.qrc b/examples/JsonTreeView/resources.qrc new file mode 100644 index 0000000..fb7afa0 --- /dev/null +++ b/examples/JsonTreeView/resources.qrc @@ -0,0 +1,5 @@ + + + sample.json + + diff --git a/examples/JsonTreeView/sample.json b/examples/JsonTreeView/sample.json new file mode 100644 index 0000000..e2217d6 --- /dev/null +++ b/examples/JsonTreeView/sample.json @@ -0,0 +1,22 @@ +{ + "firstName": "John", + "lastName": "Smith", + "age": 25, + "address": { + "streetAddress": "21 2nd Street", + "city": "New York", + "state": "NY", + "postalCode": "10021", + "owner": true + }, + "phoneNumber": [ + { + "type": "home", + "number": "212 555-1234" + }, + { + "type": "fax", + "number": "646 555-4567" + } + ] +}