Flags avec lecture JSON plus jolie

flags
Francois DELOBEL 4 weeks ago
parent 984bb271d5
commit f64ee85a74

@ -0,0 +1,22 @@
#include "countryfactory.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
void CountryFactory::load(QString filename) {
QFile file(filename);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
QJsonDocument doc(QJsonDocument::fromJson(data));
QJsonValue objet = doc["countries"];
QJsonArray liste = objet["country"].toArray();
for ( QJsonValue value : liste) {
qWarning() << value["name"].toString();
}
}

@ -0,0 +1,13 @@
#ifndef COUNTRYFACTORY_H
#define COUNTRYFACTORY_H
#include <QString>
class CountryFactory
{
public:
static void load(QString filename);
};
#endif // COUNTRYFACTORY_H

@ -5,6 +5,7 @@ QT += quick
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
countryfactory.cpp \
main.cpp
RESOURCES += qml.qrc
@ -19,3 +20,6 @@ QML_DESIGNER_IMPORT_PATH =
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
countryfactory.h

@ -1,28 +1,7 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
void load() {
QFile file(":/assets/data_en-GB.json");
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
QJsonDocument doc(QJsonDocument::fromJson(data));
QJsonValue objet = doc.object()["countries"];
QJsonArray liste = objet["country"].toArray();
for ( int i = 0 ; i < liste.size() ; ++i ) {
QJsonValue value = liste[i];
qWarning() << value["name"].toString();
}
return;
}
#include "countryfactory.h"
int main(int argc, char *argv[])
{
@ -43,7 +22,7 @@ int main(int argc, char *argv[])
},
Qt::QueuedConnection);
load();
CountryFactory::load(":/assets/data_en-GB.json");
engine.load(url);
return app.exec();

Loading…
Cancel
Save