Skip to content

Commit

Permalink
Fixed leak and moved json to resource file in json tree view example
Browse files Browse the repository at this point in the history
  • Loading branch information
Daguerreo committed Nov 22, 2021
1 parent ce656d4 commit 5a11e30
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
3 changes: 2 additions & 1 deletion examples/JsonTreeView/JsonTreeView.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
51 changes: 14 additions & 37 deletions examples/JsonTreeView/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include <QJsonDocument>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

#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){
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions examples/JsonTreeView/resources.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>sample.json</file>
</qresource>
</RCC>
22 changes: 22 additions & 0 deletions examples/JsonTreeView/sample.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}

0 comments on commit 5a11e30

Please sign in to comment.