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 --- .gitmodules | 3 +++ CMakeLists.txt | 37 ++++++++++++++++++++++++++++++------- EventsPage.qml | 1 + application.qml | 15 ++++++++++++--- external/QJsonModel | 1 + main.cpp | 22 ++++++++++++++++++++-- 6 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 .gitmodules create mode 160000 external/QJsonModel diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e9f9ab0 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/QJsonModel"] + path = external/QJsonModel + url = https://github.com/dridk/QJsonModel.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b82d00..4d985e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,14 +3,37 @@ project(itat_challange_olympics) set(CMAKE_CXX_STANDARD 17) -find_package(Qt6 REQUIRED COMPONENTS Core) +find_package(Qt6 COMPONENTS + Core REQUIRED + Gui REQUIRED + Widgets REQUIRED + Network REQUIRED + Quick REQUIRED +) + +# SET(MODULES_EXTERNAL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external) + +# include_directories(BEFORE ${MODULES_EXTERNAL_DIR}) + set(CMAKE_AUTORCC ON) -qt_add_executable(itat_challange_olympics application.qrc main.cpp) -target_link_libraries(itat_challange_olympics PRIVATE Qt6::Core) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTOUIC ON) +qt_add_executable(itat_challange_olympics application.qrc main.cpp + external/QJsonModel/include/QJsonModel.hpp + external/QJsonModel/QJsonModel.cpp) -find_package(Qt6 REQUIRED COMPONENTS Quick) -target_link_libraries(itat_challange_olympics PRIVATE Qt6::Quick) +target_include_directories(itat_challange_olympics PRIVATE + external/QJsonModel/include) -find_package(Qt6 REQUIRED COMPONENTS Network) -target_link_libraries(itat_challange_olympics PRIVATE Qt6::Network) +# add_subdirectory(${MODULES_EXTERNAL_DIR}/QJsonModel) +# target_link_libraries(itat_challange_olympics PRIVATE Qt6::QJsonModelStatic) +# target_include_directories(itat_challange_olympics PRIVATE ${CMAKE_BINARY_DIR}/include) +target_link_libraries(itat_challange_olympics +PUBLIC + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Network + Qt6::Quick +) diff --git a/EventsPage.qml b/EventsPage.qml index 772418e..f6f1bea 100644 --- a/EventsPage.qml +++ b/EventsPage.qml @@ -13,6 +13,7 @@ Page { ListView { id: listView + objectName: eventsList anchors.fill: parent topMargin: 48 leftMargin: 48 diff --git a/application.qml b/application.qml index cf7b04d..5e5d092 100644 --- a/application.qml +++ b/application.qml @@ -6,10 +6,19 @@ ApplicationWindow { height: 400 visible: true - StackView { - id: stackView + ListView { + id: listView anchors.fill: parent - initialItem: EventsPage {} + model: sports // This will reference the model added to the context + delegate: ItemDelegate { + text: modelData + } } + + // StackView { + // id: stackView + // anchors.fill: parent + // initialItem: EventsPage {} + // } } diff --git a/external/QJsonModel b/external/QJsonModel new file mode 160000 index 0000000..ed36526 --- /dev/null +++ b/external/QJsonModel @@ -0,0 +1 @@ +Subproject commit ed3652666d4954c2262eb5ceee8fda3e613f8c0c 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