Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Save QVector to JSON file

Save QVector to JSON file

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neda
    wrote on last edited by
    #1

    Hi guys,
    This code works but It'd be great if you could suggest a better code.

    "QVector<QPointF>" is the best choice for hold points? (I have a lot of points (more than 1,000,000 points) and a lot of processes on points)

    QJsonDocument JsonManager::vectorPointsToJsonDocument(QVector<QPointF> listPoints) {
        QVariantMap variantMap;
        for(int i=0;i<listPoints.size();i++){
            QJsonObject jsonObject;
            jsonObject["x"]=listPoints.at(i).x();
            jsonObject["y"]=listPoints.at(i).y();
            variantMap.insertMulti("point",jsonObject);
        }
        QJsonObject jsonObj=QJsonObject::fromVariantMap(variantMap);
    ...
    ...
    .....
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Take a look at QJsonArray and the Save Game example: http://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      N 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        Take a look at QJsonArray and the Save Game example: http://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

        N Offline
        N Offline
        neda
        wrote on last edited by neda
        #3

        @Christian-Ehrlicher
        Thanks a lot
        I see your link and write a new code.

        QJsonArray array;
            foreach(const QPointF point,listPoints){
                QJsonObject pointObject;
                pointObject["x"]=point.x();
                pointObject["y"]=point.y();
               array.append(pointObject);
            }
            QJsonObject object;
            object["points"] = array;
            QJsonDocument saveDoc(object);
            saveJson(saveDoc,fileName);
        
        1 Reply Last reply
        0
        • L Offline
          L Offline
          lautaroleon
          wrote on last edited by
          #4

          Hi. I spend some time making this small code to do a similar job in a different way. I hope someone can find it useful.

          #include <QCoreApplication>
          #include <iostream>
          #include <QVector>
          #include <QFile>
          #include <QJsonDocument>
          #include <QJsonValue>
          #include <QJsonArray>
          #include <QJsonObject>
          #include <QDateTime>
          
          QJsonDocument hacelawea(){
          
          //dummy vector
              QVector<int> mivector;
              for (int i=0;i<20;i++) {
                  mivector.append(i);
              }
          
              QJsonArray jsonarray;
              foreach(const int i, mivector){
                  QJsonValue Val(mivector[i]);
                  jsonarray.append(Val);
              }
          
              QJsonObject objetofinal;
          
              objetofinal[QDateTime::currentDateTime().toString("yyyy-MM-dd'_'hh:mm:ss")]=jsonarray;
              QJsonDocument jasonsavedoc(objetofinal);
              return jasonsavedoc;
          
          }
          
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
              std::cout<<"hola hola"<<std::endl;
          
              QFile saveFile(QStringLiteral("save.json"));
          
              if (!saveFile.open(QIODevice::ReadWrite)) {
                  qWarning("Couldn't open save file.");
              }
          
              for(int i; i<5;i++)saveFile.write(hacelawea().toJson());
          
              saveFile.close();
          
              //return a.exec();
              return 0;
          }
          
          
          
          
          
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved