feat(QJsonModel): compile and first model test

This commit is contained in:
Orangerot 2024-08-04 04:50:11 +02:00
parent 71aee728b7
commit cf013eb832
6 changed files with 67 additions and 12 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "external/QJsonModel"]
path = external/QJsonModel
url = https://github.com/dridk/QJsonModel.git

View file

@ -3,14 +3,37 @@ project(itat_challange_olympics)
set(CMAKE_CXX_STANDARD 17) 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) set(CMAKE_AUTORCC ON)
qt_add_executable(itat_challange_olympics application.qrc main.cpp) set(CMAKE_AUTOMOC ON)
target_link_libraries(itat_challange_olympics PRIVATE Qt6::Core) 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_include_directories(itat_challange_olympics PRIVATE
target_link_libraries(itat_challange_olympics PRIVATE Qt6::Quick) external/QJsonModel/include)
find_package(Qt6 REQUIRED COMPONENTS Network) # add_subdirectory(${MODULES_EXTERNAL_DIR}/QJsonModel)
target_link_libraries(itat_challange_olympics PRIVATE Qt6::Network) # 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
)

View file

@ -13,6 +13,7 @@ Page {
ListView { ListView {
id: listView id: listView
objectName: eventsList
anchors.fill: parent anchors.fill: parent
topMargin: 48 topMargin: 48
leftMargin: 48 leftMargin: 48

View file

@ -6,10 +6,19 @@ ApplicationWindow {
height: 400 height: 400
visible: true visible: true
StackView { ListView {
id: stackView id: listView
anchors.fill: parent 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 {}
// }
} }

1
external/QJsonModel vendored Submodule

@ -0,0 +1 @@
Subproject commit ed3652666d4954c2262eb5ceee8fda3e613f8c0c

View file

@ -21,6 +21,8 @@
#include <QDebug> #include <QDebug>
// #include <iostream> // #include <iostream>
#include "QJsonModel.hpp"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
@ -28,12 +30,24 @@ int main(int argc, char *argv[])
QQmlEngine engine; QQmlEngine engine;
QQmlContext *objectContext = new QQmlContext(engine.rootContext()); 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"); QQmlComponent component(&engine, "application.qml");
QObject *object = component.create(objectContext); QObject *object = component.create(objectContext);
QObject *eventsList = object->findChild<QObject*>("eventsList");
QQmlContext *componentContext = component.creationContext();
// ... delete object and objectContext when necessary // ... delete object and objectContext when necessary
// create custom temporary event loop on stack // create custom temporary event loop on stack
QEventLoop eventLoop; QEventLoop eventLoop;
@ -53,12 +67,16 @@ int main(int argc, char *argv[])
QString strReply = (QString)reply->readAll(); QString strReply = (QString)reply->readAll();
//parse json //parse json
// qDebug() << "Response:" << strReply; // 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(); QJsonObject jsonObj = jsonResponse.object();
// componentContext->setContextProperty("sports", model);
qDebug() << "Competitor:" << jsonObj["units"][0]["competitors"][0]["name"].toString(); qDebug() << "Competitor:" << jsonObj["units"][0]["competitors"][0]["name"].toString();
delete reply; delete reply;