How do you get the QTableView data to json?



  • There's a passing cycle all over the table:

    for (int i = 0; i < m_model->rowCount(); ++i) {
        for (int j = 0; j < m_model->columnCount(); ++j) {
           //Вывод данных в переменную
        }
    }
    

    So that's what type of data is needed to correctly release the information from the table to json file. And how to record it in json. With json downloaded into the table, I sorted out the retention. Can you help me?



  • There are two options.

    1. Handwriting the line

      QString s="[";
      for (int i = 0; i < m_model->rowCount(); ++i) {
          if (i==0) s+="[";else s+=",[";
          for (int j = 0; j < m_model->columnCount(); ++j) {
              if (j!=0) s+=",";
              s+="\""+m_model->item (i, j)->text()+"\"";
              //Вывод данных в переменную
          }
          s+="]";
      }
      s+="]";
      
    2. Use json classes

      QJsonArray tab;
      for (int i = 0; i < m_model->rowCount(); ++i) {
          QJsonArray row;
          if (i==0) s+="[";else s+=",[";
          for (int j = 0; j < m_model->columnCount(); ++j) {
              row.append(m_model->item (i, j)->text());
          }
          tab.append(row);
      }
      s+="]";
      QJsonDocument doc;
      doc.setObject (tab);
      QString s = QString (doc.toJson());
      



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2