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. QVXYModelMapper does not update chart (x-axis?)

QVXYModelMapper does not update chart (x-axis?)

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.1k 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.
  • O Offline
    O Offline
    oliverleo
    wrote on last edited by
    #1

    Hello,

    i want to show a Line chart, where the X-Axis is time based. The data is added while the application is running.
    At the moment I use an own QAbstractTableModel implementation, where I add rows when the program runs. The table is setup with some points, when the program starts.

    To connect the TableModel to the chart I use the QVXYModelMapper class.
    I also have a basic TableView which just shows the table data.
    Both views, show the points that I setup when the program starts. But only the TableView updates its data, when new rows are added. The chart shows only the values from the start of the program and does not update. What do I need to change?

    Here is my Table implementation:

    #include "VoltagesTimeModel.h"
    #include <QDebug>
    
    VoltagesTimeModel::VoltagesTimeModel() {
    
        setup();
    
    }
    
    int VoltagesTimeModel::rowCount(const QModelIndex &parent) const {
        return tableData.length();
    }
    
    int VoltagesTimeModel::columnCount(const QModelIndex &parent) const {
        return 4;
    }
    
    QVariant VoltagesTimeModel::headerData(int section, Qt::Orientation orientation, int role) const {
        if(role != Qt::DisplayRole) {
            return QVariant();
        }
        if(orientation == Qt::Horizontal) {
            if(section == 0) {
                return "Time";
            } else if (section == 1) {
                return "Average";
            } else if(section == 2) {
                return "Min";
            } else if(section == 3) {
                return "Max";
            }
        }
        return QVariant();
    }
    
    QVariant VoltagesTimeModel::dataMap(const QModelIndex &index, int role) const {
        if(!index.isValid())
            return QVariant();
    
        if(index.row() >= tableData.length() || index.row() < 0) {
            return QVariant();
        }
        if(role == Qt::DisplayRole) {
            if(index.column() == 0) {
                return tableData.at(index.row()).time;
            } else if(index.column() == 1) {
                return tableData.at(index.row()).average;
            } else if(index.column() == 2) {
                return tableData.at(index.row()).min;
            } else if(index.column() == 3) {
                return tableData.at(index.row()).max;
            }
        }
        return QVariant();
    }
    
    void VoltagesTimeModel::setData(Statistics *stat) {
        qint64 time = QDateTime::currentMSecsSinceEpoch();
    
        insertRows(0, 1, QModelIndex());
        int row = tableData.size()-1;
        setData(index(row,0), time, Qt::EditRole);
        setData(index(row,1), stat->getAverage(),Qt::EditRole);
        setData(index(row,2), stat->getMin().second, Qt::EditRole);
        setData(index(row,3), stat->getMax().second, Qt::EditRole);
    
    }
    
    void VoltagesTimeModel::setup() {
        qint64 now = QDateTime::currentMSecsSinceEpoch() ;
        qint64 time = now -10;
        for(qint64 i= time; i < now; i++) {
            Data data;
            data.time = i;
            double random = 2 + ((double)rand() / (double)RAND_MAX) * 2.5;
            data.average = random;
            data.max = 3.4;
            data.min = 3;
            tableData.append(data);
        }
    }
    
    bool VoltagesTimeModel::insertRows(int row, int count, const QModelIndex &parent) {
        beginInsertRows(QModelIndex(), row, row+count-1);
    
        for(int i=0; i< count; i++) {
            Data data;
            data.time = i;
            double random = 2 + ((double)rand() / (double)RAND_MAX) * 2.5;
            data.average = 0;
            data.max = 0;
            data.min = 0;
            tableData.append(data);
        }
        endInsertRows();
        return true;
    }
    
    bool VoltagesTimeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
        if(index.isValid() && role == Qt::EditRole) {
            int row = index.row();
            Data data = tableData.at(row);
            if(index.column() == 0) {
                data.time = value.toLongLong();
            } else if(index.column() == 1) {
                data.average = value.toDouble();
            } else if(index.column() == 2) {
                data.min = value.toDouble();
            } else if(index.column() == 3) {
                data.max = value.toDouble();
            }
            tableData.replace(row, data);
            emit dataChanged(index, index);
            return true;
        }
        return false;
    }
    
    

    And here where I create the ChartView:
    QLineSeries *series = new QLineSeries;
    series->setName("Average");
    QVXYModelMapper *mapper = new QVXYModelMapper(this);
    mapper->setXColumn(0);
    mapper->setYColumn(1);
    mapper->setSeries(series);
    mapper->setModel(processer->getSlaveVoltagesTimeModel());
    mapper->setFirstRow(0);
    mapper->setRowCount(10000);
    QChart *chart = new QChart();
    chart->addSeries(series);

    chart->createDefaultAxes();
    
    chart->axisY()->setRange(2.0, 4.5);
    
    QDateTimeAxis *axis = new QDateTimeAxis;
    axis->setTickCount(10);
    axis->setFormat("hh:mm:ss:zzz");
    
    chart->setAxisX(axis);
    series->attachAxis(axis);
    
    chart->setAnimationOptions(QChart::AllAnimations);
    

    QChartView *voltageTimeChartView = new QChartView(chart);

    Can somebody help?

    1 Reply Last reply
    1
    • O Offline
      O Offline
      o.semmler
      wrote on last edited by
      #2

      Hello,

      I have the same problem.
      Have you solved it?

      1 Reply Last reply
      0
      • AndySA Offline
        AndySA Offline
        AndyS
        Moderators
        wrote on last edited by
        #3

        Nothing stands out as being obviously wrong at least. Can you provide a small example that shows the problem?

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

        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