Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QAbstractTableModel to JSON



  • Hi,
    I use "VXYModelMapper" and "QAbstractTableModel" for draw real time chart.
    I wanna save points of chart in JSON file.
    Please guide me.

    QML:

    VXYModelMapper {          
                xColumn: 0
                yColumn: 1
                model: chartModel
      }
    

    chartmodel:

    class ChartModel : public QAbstractTableModel{
        Q_OBJECT
    
        ......
    ......
    .....
    
        QVector<QPair<double,double>> vecChartPoints;
    public:
        explicit ChartModel(QObject *parent=nullptr);
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        int columnCount(const QModelIndex &parent = QModelIndex()) const override;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
        QPair<double,double> dataRow(int row);
        ...
    ....
    public slots:
       .....
    ......
    
        void appendData(double const xVal,double const yVal);
    signals:
        .....
    };
    
    

    chartmodel.cpp:

    void ChartModel::appendData(double const xVal,double const yVal)
    {
        setXMin(qMin(m_xMin, xVal));
        setXMax(qMax(m_xMax, xVal));
        setYMin(qMin(m_yMin, yVal));
        setYMax(qMax(m_yMax, yVal));
        int const vecSize = vecChartPoints.size();
        beginInsertRows(QModelIndex (), vecSize, vecSize);
        vecChartPoints.append(qMakePair<double, double>(xVal, yVal));
        endInsertRows();
    }
    

  • Qt Champions 2017

    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.



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



  • 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) ).


  • Moderators

    @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.



  • @JKSH

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


  • Moderators

    @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".



  • @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


  • Moderators

    @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?



  • @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)


  • Lifetime Qt Champion

    @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).


Log in to reply