This more or less what Qt do with ui which xml file convert to .h. What I'm trying to do is get List from Json file
_____________JsonFile_____________
{
"common_message": {
"Id_message": "NA"
"Lenght" : "NA",
"bit" : "NA",[B][/B]
etc
}
}
{
"common_message": {
"Id_message": "NA"
"Lenght" : "NA",
"bit" : "NA",[B][/B]
etc
}
}
To copy to clipboard, switch view to plain text mode
_________________________________
I want to display Id_message in qstring and the rest of the field
___________________main.cpp______________
this,
tr("Charger les resources HW"),
"C//workspace_llb_27-05-2016/10_Terminal_Generic/configuration",
"json document (*.json)"
);
{
return;
}
in = file.readAll();
file.close();
// qWarning() << in;
m_configHw = QJsonDocument::fromJson(in.toUtf8());
buildDock();
}
// TODO
void MainWindow::buildDock()
{
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
QJsonObject jsonObject = m_configHw.object();
// QStringList list = jsonObject.keys();
// QVariantList list = jsonArray.toVariantList();
// QJsonArray jsonArray = jsonObject.value("common_message").toArray();
// QStringList list = jsonObject.value("common_message").toString();
QJsonArray jsonArray = jsonObject.value("common_message").toArray();
QVariantList list = jsonArray.toVariantList();
// for(int i(0); i< jsonArray.size(), i++){
// QJsonObject obj = jsonArray[i].toObject();
// list.append(obj.);
// }
// foreach(const QJsonValue & value, jsonArray){
// QJsonObject obj = value.toObject();
// list = obj.keys();
// }
foreach
(const QVariant & value, jsonArray
){
QVariantMap obj = value.toMap();
qDebug() << obj.value("Id_message");
}
// QVariantMap pair = jsonObject.toVariantMap();
// QAbstractItemModel *model = new QStringListModel(list.toStdList());
// QListView *view = new QListView;
// view->setModel(model);
// ui->dockWidgetContents->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
// dock->setWidget(view);
addDockWidget(Qt::LeftDockWidgetArea, dock);
}
QString filename = QFileDialog::getOpenFileName(
this,
tr("Charger les resources HW"),
"C//workspace_llb_27-05-2016/10_Terminal_Generic/configuration",
"json document (*.json)"
);
QFile file(filename);
QString in;
if (!file.open(QIODevice::ReadWrite))
{
QMessageBox::information(0,"info",file.errorString());
return;
}
in = file.readAll();
file.close();
// qWarning() << in;
m_configHw = QJsonDocument::fromJson(in.toUtf8());
buildDock();
}
// TODO
void MainWindow::buildDock()
{
QDockWidget *dock = new QDockWidget("test",this);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
QJsonObject jsonObject = m_configHw.object();
// QStringList list = jsonObject.keys();
// QVariantList list = jsonArray.toVariantList();
// QJsonArray jsonArray = jsonObject.value("common_message").toArray();
// QStringList list = jsonObject.value("common_message").toString();
QJsonArray jsonArray = jsonObject.value("common_message").toArray();
QVariantList list = jsonArray.toVariantList();
// for(int i(0); i< jsonArray.size(), i++){
// QJsonObject obj = jsonArray[i].toObject();
// list.append(obj.);
// }
// foreach(const QJsonValue & value, jsonArray){
// QJsonObject obj = value.toObject();
// list = obj.keys();
// }
foreach(const QVariant & value, jsonArray){
QVariantMap obj = value.toMap();
qDebug() << obj.value("Id_message");
}
// QVariantMap pair = jsonObject.toVariantMap();
// QAbstractItemModel *model = new QStringListModel(list.toStdList());
// QListView *view = new QListView;
// view->setModel(model);
// ui->dockWidgetContents->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
// dock->setWidget(view);
addDockWidget(Qt::LeftDockWidgetArea, dock);
}
To copy to clipboard, switch view to plain text mode
Sorry It's a lot messy but my aim is to display a list of json and a least in my IHM change the values in time. how could I do a list from the field in my json ?
If you don't get it what I'm trying to do ask me question !
Thanks for any advice
Bookmarks