From cf013eb832f323cb9de6b114038233b77d115bc8 Mon Sep 17 00:00:00 2001 From: Orangerot Date: Sun, 4 Aug 2024 04:50:11 +0200 Subject: feat(QJsonModel): compile and first model test --- main.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 6fc38fc..aa833f5 100644 --- a/main.cpp +++ b/main.cpp @@ -21,6 +21,8 @@ #include // #include +#include "QJsonModel.hpp" + int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); @@ -28,12 +30,24 @@ int main(int argc, char *argv[]) QQmlEngine engine; QQmlContext *objectContext = new QQmlContext(engine.rootContext()); + + QStringList dataList = { + "Item 1", + "Item 2", + "Item 3", + "Item 4" + }; + + objectContext->setContextProperty("sports", QVariant::fromValue(dataList)); + QQmlComponent component(&engine, "application.qml"); QObject *object = component.create(objectContext); - + QObject *eventsList = object->findChild("eventsList"); + QQmlContext *componentContext = component.creationContext(); // ... delete object and objectContext when necessary + // create custom temporary event loop on stack QEventLoop eventLoop; @@ -53,12 +67,16 @@ int main(int argc, char *argv[]) QString strReply = (QString)reply->readAll(); + //parse json // qDebug() << "Response:" << strReply; - QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); + QJsonModel * model = new QJsonModel(strReply.toUtf8()); + QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); QJsonObject jsonObj = jsonResponse.object(); + // componentContext->setContextProperty("sports", model); + qDebug() << "Competitor:" << jsonObj["units"][0]["competitors"][0]["name"].toString(); delete reply; -- cgit v1.2.3