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. QAbstractTableModel to JSON
Qt 6.11 is out! See what's new in the release blog

QAbstractTableModel to JSON

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 2.3k Views 1 Watching
  • 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #2

    Nothing exist directly. You need to add the methods inside your custom model and call JSON apis to store the data. You can use QJsonDocument etc classes to do this.

    Dheerendra
    @Community Service
    Certified Qt Specialist
    http://www.pthinks.com

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #3

      See the "Model Serialisation" part of this library. You can find an example usage here

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • N Offline
        N Offline
        neda
        wrote on last edited by neda
        #4

        For save chart I use this method:
        I add point to ModelChart and another vector(qvector<qpoint>) in same time and for save I use this code.

        QJsonArray jsonArray;
        foreach(const QPointF point,listPoints){
        QJsonObject pointObject;
        pointObject["x"]=point.x();
        pointObject["y"]=point.y();
        jsonArray.append(pointObject);
        }
        jsonTest["points"] = jsonArray;
        QJsonDocument saveDoc(jsonTest);
        

        If you use the better method and could guide me , I would really really appreciate it (I have a lot of points (more than 1,000,000 points) ).

        JKSHJ 1 Reply Last reply
        0
        • N neda

          For save chart I use this method:
          I add point to ModelChart and another vector(qvector<qpoint>) in same time and for save I use this code.

          QJsonArray jsonArray;
          foreach(const QPointF point,listPoints){
          QJsonObject pointObject;
          pointObject["x"]=point.x();
          pointObject["y"]=point.y();
          jsonArray.append(pointObject);
          }
          jsonTest["points"] = jsonArray;
          QJsonDocument saveDoc(jsonTest);
          

          If you use the better method and could guide me , I would really really appreciate it (I have a lot of points (more than 1,000,000 points) ).

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #5

          @neda said in QAbstractTableModel to JSON:

          For save chart I use this method:
          I add point to ModelChart and another vector(qvector<qpoint>) in same time

          ...

          (I have a lot of points (more than 1,000,000 points) ).

          Is there are reason why you want to use JSON? JSON is not ideal for storing the data from large charts.

          A CSV file is easiest to read and write. You only need 2 columns (X and Y)

          QFile file("data.csv");
          file.open(QFile::WriteOnly|QFile::Text);
          file.write("X,Y\n"); // Label the columns
          
          for (const QPointF &point : listPoints) {
              auto line = QByteArray::number(point.x()) + ',' + QByteArray::number(point.y()) + '\n';
              file.write(line);
          }
          

          EDIT: As a bonus, you can open the CSV file directly in Microsoft Excel or LibreOffice Calc and plot it.

          If a CSV file with 1,000,000 points is too big for you, consider a binary data file like SQLite.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          N 1 Reply Last reply
          3
          • JKSHJ JKSH

            @neda said in QAbstractTableModel to JSON:

            For save chart I use this method:
            I add point to ModelChart and another vector(qvector<qpoint>) in same time

            ...

            (I have a lot of points (more than 1,000,000 points) ).

            Is there are reason why you want to use JSON? JSON is not ideal for storing the data from large charts.

            A CSV file is easiest to read and write. You only need 2 columns (X and Y)

            QFile file("data.csv");
            file.open(QFile::WriteOnly|QFile::Text);
            file.write("X,Y\n"); // Label the columns
            
            for (const QPointF &point : listPoints) {
                auto line = QByteArray::number(point.x()) + ',' + QByteArray::number(point.y()) + '\n';
                file.write(line);
            }
            

            EDIT: As a bonus, you can open the CSV file directly in Microsoft Excel or LibreOffice Calc and plot it.

            If a CSV file with 1,000,000 points is too big for you, consider a binary data file like SQLite.

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

            @JKSH

            Thanks for your reply.
            Between .dat or CSV file, Which is better and faster to read and write?

            JKSHJ 1 Reply Last reply
            0
            • N neda

              @JKSH

              Thanks for your reply.
              Between .dat or CSV file, Which is better and faster to read and write?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #7

              @neda said in QAbstractTableModel to JSON:

              Between .dat or CSV file, Which is better and faster to read and write?

              Binary is usually faster than text (CSV).

              You need to tell us your criteria for what makes something "Better".

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              N 1 Reply Last reply
              0
              • JKSHJ JKSH

                @neda said in QAbstractTableModel to JSON:

                Between .dat or CSV file, Which is better and faster to read and write?

                Binary is usually faster than text (CSV).

                You need to tell us your criteria for what makes something "Better".

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

                @JKSH said in QAbstractTableModel to JSON:

                You need to tell us your criteria for what makes something "Better".

                Thank you dear friend. I have real time chart (QML). I would like to save and load this chart fast and without delay.
                Although I still do not know what is best way for draw a real time chart. I use below example.
                https://forum.qt.io/topic/77439/can-someone-provide-a-working-example-of-vxymodelmapper-with-lineseries-chart/2

                JKSHJ 1 Reply Last reply
                0
                • N neda

                  @JKSH said in QAbstractTableModel to JSON:

                  You need to tell us your criteria for what makes something "Better".

                  Thank you dear friend. I have real time chart (QML). I would like to save and load this chart fast and without delay.
                  Although I still do not know what is best way for draw a real time chart. I use below example.
                  https://forum.qt.io/topic/77439/can-someone-provide-a-working-example-of-vxymodelmapper-with-lineseries-chart/2

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #9

                  @neda said in QAbstractTableModel to JSON:

                  I would like to save and load this chart fast and without delay.

                  If your main concern is to save and load quickly, then you definitely shouldn't use JSON.

                  Also, I saw your post at https://forum.qt.io/topic/97030/insert-rows-in-qabstracttablemodel-is-very-slow/ -- since you are concerned about speed, then you should profile your code to see exactly what's slow. Use QElapsedTimer to measure how long certain parts of your code take.

                  Although I still do not know what is best way for draw a real time chart. I use below example.
                  https://forum.qt.io/topic/77439/can-someone-provide-a-working-example-of-vxymodelmapper-with-lineseries-chart/2

                  I'm not experienced with Qt Charts so I'm afraid can't give advice on the best way to use it.

                  However, I noticed that you mentioned both real time charts and saving/loading charts (which is not real-time). Can you describe both use-cases in more detail? Where does your data come from?

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  N 1 Reply Last reply
                  2
                  • JKSHJ JKSH

                    @neda said in QAbstractTableModel to JSON:

                    I would like to save and load this chart fast and without delay.

                    If your main concern is to save and load quickly, then you definitely shouldn't use JSON.

                    Also, I saw your post at https://forum.qt.io/topic/97030/insert-rows-in-qabstracttablemodel-is-very-slow/ -- since you are concerned about speed, then you should profile your code to see exactly what's slow. Use QElapsedTimer to measure how long certain parts of your code take.

                    Although I still do not know what is best way for draw a real time chart. I use below example.
                    https://forum.qt.io/topic/77439/can-someone-provide-a-working-example-of-vxymodelmapper-with-lineseries-chart/2

                    I'm not experienced with Qt Charts so I'm afraid can't give advice on the best way to use it.

                    However, I noticed that you mentioned both real time charts and saving/loading charts (which is not real-time). Can you describe both use-cases in more detail? Where does your data come from?

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

                    @JKSH said in QAbstractTableModel to JSON:

                    Where does your data come from?

                    Thank you for your reply.
                    I read the data from serial port(200 sample per second) and draw real time chart at the same time. After finish receive data from port I should save this chart and I should can load it again at another time.
                    I have problem with both of them (plotting real time charts and fast loading saved chart)

                    jsulmJ 1 Reply Last reply
                    0
                    • N neda

                      @JKSH said in QAbstractTableModel to JSON:

                      Where does your data come from?

                      Thank you for your reply.
                      I read the data from serial port(200 sample per second) and draw real time chart at the same time. After finish receive data from port I should save this chart and I should can load it again at another time.
                      I have problem with both of them (plotting real time charts and fast loading saved chart)

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @neda One thing you can try is to not to update the chart 200 times per second as most displays work at 60Hz, some a bit above 100Hz. So, try to update only 60 times per second, or even lower at 24Hz (this is what our eyes perceive as smooth).

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2

                      • Login

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