PDA

View Full Version : Using QJson for read and display A list into QStandardItemModel



Lokl
6th July 2016, 14:53
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",
etc
}
}
_________________________________

I want to display Id_message in qstring and the rest of the field

___________________main.cpp______________



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);
}


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

anda_skoa
6th July 2016, 19:02
You are trying to convert the value of "common_message" into an array, but it is not an array.
"common_message" is an object.

Also, don't convert the return value of QFile::readAll() needlessly into a QString and then back into a QByteArray.

Cheers,
_

Lokl
7th July 2016, 09:12
oh thanks a lot indeed. I just did what you said :
[CODE]
QJsonObject JsonObject = m_configHw.Object();
QStringList list;

foreach(const QJsonValue & value, jsonObject){
QJsonObject obj = value.toObject();
list.append(obj.keys());
}

QAbstractItemModel *model = new QStringListModel(list);

QListView *view = new QListView;

view->setModel(model);
dock->setWidget(view);
[\CODE]

yeah I heard about the readall(). I'm going to change it
Cheers