feat(JSON): test QJsonModel
This commit is contained in:
		
							parent
							
								
									cf013eb832
								
							
						
					
					
						commit
						efeec3b99e
					
				|  | @ -6,13 +6,14 @@ ApplicationWindow { | ||||||
|   height: 400 |   height: 400 | ||||||
|   visible: true |   visible: true | ||||||
| 
 | 
 | ||||||
|   ListView { |   TreeView { | ||||||
|     id: listView |     id: listView | ||||||
|     anchors.fill: parent |     anchors.fill: parent | ||||||
|     model: sports // This will reference the model added to the context |     model: sports // This will reference the model added to the context | ||||||
|     delegate: ItemDelegate {  |     delegate: ItemDelegate {  | ||||||
|       text: modelData  |       text: eventName | ||||||
|     } |     } | ||||||
|  |     Component.onCompleted : console.log(Object.keys(sports.data)) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   // StackView { |   // StackView { | ||||||
|  |  | ||||||
							
								
								
									
										24
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								main.cpp
									
									
									
									
									
								
							|  | @ -19,6 +19,7 @@ | ||||||
| 
 | 
 | ||||||
| // console output
 | // console output
 | ||||||
| #include <QDebug> | #include <QDebug> | ||||||
|  | #include <qlogging.h> | ||||||
| // #include <iostream>
 | // #include <iostream>
 | ||||||
| 
 | 
 | ||||||
| #include "QJsonModel.hpp" | #include "QJsonModel.hpp" | ||||||
|  | @ -38,12 +39,12 @@ int main(int argc, char *argv[]) | ||||||
|       "Item 4" |       "Item 4" | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     objectContext->setContextProperty("sports", QVariant::fromValue(dataList)); |     //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"); |     // QObject *eventsList = object->findChild<QObject*>("eventsList");
 | ||||||
|     QQmlContext *componentContext = component.creationContext(); |     // QQmlContext *componentContext = component.creationContext();
 | ||||||
|     // ... delete object and objectContext when necessary
 |     // ... delete object and objectContext when necessary
 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -70,12 +71,21 @@ int main(int argc, char *argv[]) | ||||||
| 
 | 
 | ||||||
|         //parse json
 |         //parse json
 | ||||||
|         // qDebug() << "Response:" << strReply;
 |         // qDebug() << "Response:" << strReply;
 | ||||||
| 
 |  | ||||||
|         QJsonModel * model = new QJsonModel(strReply.toUtf8()); |  | ||||||
|         QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); |         QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); | ||||||
|         QJsonObject jsonObj = jsonResponse.object(); |         QJsonObject jsonObj = jsonResponse.object(); | ||||||
| 
 | 
 | ||||||
|  |         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);
 |         // componentContext->setContextProperty("sports", model);
 | ||||||
|  | 
 | ||||||
|  |         // objectContext->setContextProperty("sports", QVariant::fromValue(dataList));
 | ||||||
|  | 
 | ||||||
|  |         QQmlComponent component(&engine, "application.qml"); | ||||||
|  |         QObject *object = component.create(objectContext); | ||||||
|          |          | ||||||
|         qDebug() << "Competitor:" << jsonObj["units"][0]["competitors"][0]["name"].toString(); |         qDebug() << "Competitor:" << jsonObj["units"][0]["competitors"][0]["name"].toString(); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue