itat_challenge/main.cpp

109 lines
3 KiB
C++
Raw Normal View History

2024-07-26 17:49:18 +02:00
#include <QGuiApplication>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlComponent>
2024-06-27 12:18:32 +02:00
2024-07-26 19:17:16 +02:00
// networking
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QUrlQuery>
// json parsing
#include <QJsonValue>
#include <QJsonDocument>
#include <QJsonObject>
#include <QVariantMap>
#include <QJsonArray>
// console output
#include <QDebug>
2024-08-11 20:54:39 +02:00
#include <qlogging.h>
2024-07-26 19:17:16 +02:00
// #include <iostream>
#include "QJsonModel.hpp"
#include <QTreeView>
#include <QApplication>
2024-07-26 17:49:18 +02:00
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
2024-07-26 17:49:18 +02:00
QQmlEngine engine;
QQmlContext *objectContext = new QQmlContext(engine.rootContext());
QStringList dataList = {
"Item 1",
"Item 2",
"Item 3",
"Item 4"
};
2024-08-11 20:54:39 +02:00
//objectContext->setContextProperty("sports", QVariant::fromValue(dataList));
2024-08-11 20:54:39 +02:00
//QQmlComponent component(&engine, "application.qml");
//QObject *object = component.create(objectContext);
// QObject *eventsList = object->findChild<QObject*>("eventsList");
// QQmlContext *componentContext = component.creationContext();
2024-07-26 17:49:18 +02:00
// ... delete object and objectContext when necessary
2024-07-26 19:17:16 +02:00
2024-07-26 19:17:16 +02:00
// create custom temporary event loop on stack
QEventLoop eventLoop;
// "quit()" the event-loop, when the network request "finished()"
QNetworkAccessManager mgr;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QString endpoint { "https://sph-s-api.olympics.com/summer/schedules/api/ENG/schedule/discipline/ARC" };
// the HTTP request
QNetworkRequest req( (QUrl( endpoint )) );
QNetworkReply *reply = mgr.get(req);
eventLoop.exec(); // blocks stack until "finished()" has been called
if (reply->error() == QNetworkReply::NoError) {
//success
QString strReply = (QString)reply->readAll();
2024-07-26 19:17:16 +02:00
//parse json
// qDebug() << "Response:" << strReply;
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
2024-07-26 19:17:16 +02:00
QJsonObject jsonObj = jsonResponse.object();
2024-08-11 20:54:39 +02:00
QJsonModel * model = new QJsonModel();
// qDebug() << QJsonDocument(jsonObj["units"].toArray()).toJson();
// model->loadJson(QJsonDocument(jsonObj["units"].toArray()).toJson());
model->loadJson(strReply.toUtf8());
objectContext->setContextProperty("sports", QVariant::fromValue(model));
// componentContext->setContextProperty("sports", model);
2024-08-11 20:54:39 +02:00
// objectContext->setContextProperty("sports", QVariant::fromValue(dataList));
QQmlComponent component(&engine, "application.qml");
QObject *object = component.create(objectContext);
QTreeView * view = new QTreeView;
view->setModel(model);
view->show();
2024-07-26 19:17:16 +02:00
qDebug() << "Competitor:" << jsonObj["units"][0]["competitors"][0]["name"].toString();
delete reply;
}
else {
//failure
qDebug() << "Failure" <<reply->errorString();
delete reply;
}
2024-07-26 17:49:18 +02:00
return app.exec();
2024-06-27 12:18:32 +02:00
}