feat(QJsonModel): compile and first model test
This commit is contained in:
parent
71aee728b7
commit
cf013eb832
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
[submodule "external/QJsonModel"]
|
||||
path = external/QJsonModel
|
||||
url = https://github.com/dridk/QJsonModel.git
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -13,6 +13,7 @@ Page {
|
|||
|
||||
ListView {
|
||||
id: listView
|
||||
objectName: eventsList
|
||||
anchors.fill: parent
|
||||
topMargin: 48
|
||||
leftMargin: 48
|
||||
|
|
|
@ -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 {}
|
||||
// }
|
||||
}
|
||||
|
||||
|
|
1
external/QJsonModel
vendored
Submodule
1
external/QJsonModel
vendored
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit ed3652666d4954c2262eb5ceee8fda3e613f8c0c
|
22
main.cpp
22
main.cpp
|
@ -21,6 +21,8 @@
|
|||
#include <QDebug>
|
||||
// #include <iostream>
|
||||
|
||||
#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<QObject*>("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;
|
||||
|
|
Loading…
Reference in a new issue