Results 1 to 3 of 3

Thread: Using QJson for read and display A list into QStandardItemModel

  1. #1
    Join Date
    Apr 2016
    Posts
    16
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Using QJson for read and display A list into QStandardItemModel

    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_____________
    Qt Code:
    1. {
    2. "common_message": {
    3. "Id_message": "NA"
    4. "Lenght" : "NA",
    5. "bit" : "NA",[B][/B]
    6. etc
    7. }
    8. }
    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______________

    Qt Code:
    1. QString filename = QFileDialog::getOpenFileName(
    2.  
    3. this,
    4.  
    5. tr("Charger les resources HW"),
    6.  
    7. "C//workspace_llb_27-05-2016/10_Terminal_Generic/configuration",
    8.  
    9. "json document (*.json)"
    10.  
    11. );
    12.  
    13. QFile file(filename);
    14.  
    15. QString in;
    16.  
    17.  
    18.  
    19. if (!file.open(QIODevice::ReadWrite))
    20.  
    21. {
    22.  
    23. QMessageBox::information(0,"info",file.errorString());
    24.  
    25. return;
    26.  
    27. }
    28.  
    29. in = file.readAll();
    30.  
    31. file.close();
    32.  
    33. // qWarning() << in;
    34.  
    35. m_configHw = QJsonDocument::fromJson(in.toUtf8());
    36.  
    37. buildDock();
    38.  
    39. }
    40.  
    41.  
    42.  
    43. // TODO
    44. void MainWindow::buildDock()
    45.  
    46. {
    47.  
    48. QDockWidget *dock = new QDockWidget("test",this);
    49.  
    50. dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    51.  
    52.  
    53.  
    54. QJsonObject jsonObject = m_configHw.object();
    55.  
    56.  
    57.  
    58. // QStringList list = jsonObject.keys();
    59.  
    60. // QVariantList list = jsonArray.toVariantList();
    61.  
    62. // QJsonArray jsonArray = jsonObject.value("common_message").toArray();
    63.  
    64. // QStringList list = jsonObject.value("common_message").toString();
    65.  
    66. QJsonArray jsonArray = jsonObject.value("common_message").toArray();
    67.  
    68.  
    69.  
    70. QVariantList list = jsonArray.toVariantList();
    71.  
    72. // for(int i(0); i< jsonArray.size(), i++){
    73.  
    74. // QJsonObject obj = jsonArray[i].toObject();
    75.  
    76. // list.append(obj.);
    77.  
    78. // }
    79.  
    80.  
    81.  
    82. // foreach(const QJsonValue & value, jsonArray){
    83.  
    84. // QJsonObject obj = value.toObject();
    85.  
    86. // list = obj.keys();
    87.  
    88. // }
    89.  
    90. foreach(const QVariant & value, jsonArray){
    91.  
    92. QVariantMap obj = value.toMap();
    93.  
    94. qDebug() << obj.value("Id_message");
    95.  
    96. }
    97.  
    98.  
    99.  
    100.  
    101.  
    102. // QVariantMap pair = jsonObject.toVariantMap();
    103.  
    104.  
    105.  
    106. // QAbstractItemModel *model = new QStringListModel(list.toStdList());
    107.  
    108. // QListView *view = new QListView;
    109.  
    110. // view->setModel(model);
    111.  
    112.  
    113.  
    114. // ui->dockWidgetContents->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    115.  
    116. // dock->setWidget(view);
    117.  
    118. addDockWidget(Qt::LeftDockWidgetArea, dock);
    119. }
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QJson for read and display A list into QStandardItemModel

    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,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    16
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using QJson for read and display A list into QStandardItemModel

    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

Similar Threads

  1. Replies: 6
    Last Post: 10th December 2012, 11:15
  2. Replies: 4
    Last Post: 15th June 2012, 00:33
  3. [QStandardItemModel] read from a different thread
    By mentalmushroom in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2011, 10:33
  4. qtableview QStandardItemModel read only
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2008, 17:42
  5. Display data from QStandardItemModel in QTreeView
    By jprice01801 in forum Qt Programming
    Replies: 7
    Last Post: 10th January 2007, 10:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.