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

@ -1,28 +1,7 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
void load() { #include "countryfactory.h"
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;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -43,7 +22,7 @@ int main(int argc, char *argv[])
}, },
Qt::QueuedConnection); Qt::QueuedConnection);
load(); CountryFactory::load(":/assets/data_en-GB.json");
engine.load(url); engine.load(url);
return app.exec(); return app.exec();

Loading…
Cancel
Save