After changing the model value, the tableview value is changed, but the chart is the same.
-
CustomTableModel::CustomTableModel(QObject *parent) : QAbstractTableModel(parent) { m_columnCount = 6; m_rowCount = 12; //// m_data for (int i = 0; i < m_rowCount; i++) { QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount); m_data.append(dataVec); } // for (int k = 0; k < dataVec->size(); k++) { // if (k % 2 == 0) // dataVec->replace(k, i * 50 + QRandomGenerator::global()->bounded(20)); // else // dataVec->replace(k, QRandomGenerator::global()->bounded(100)); // } // m_data.append(dataVec); //} } CustomTableModel::~CustomTableModel() { qDeleteAll(m_data); } int CustomTableModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) return m_data.count(); } int CustomTableModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent) return m_columnCount; } QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) return QVariant(); if (orientation == Qt::Horizontal) return QString("201%1").arg(section); else return QString("%1").arg(section + 1); } QVariant CustomTableModel::data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) { return m_data[index.row()]->at(index.column()); } else if (role == Qt::EditRole) { return m_data[index.row()]->at(index.column()); } else if (role == Qt::BackgroundRole) { for (const QRect &rect : m_mapping) { if (rect.contains(index.column(), index.row())) return QColor(m_mapping.key(rect)); } // cell not mapped return white color return QColor(Qt::white); } return QVariant(); } bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { m_data[index.row()]->replace(index.column(), value.toDouble()); emit dataChanged(index, index); return true; } return false; } Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const { return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; } void CustomTableModel::addData(int nRow, int nCol, int data) { //QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount); //m_data } void CustomTableModel::addMapping(QString color, QRect area) { m_mapping.insertMulti(color, area); } TableWidget::TableWidget(QWidget *parent) : QWidget(parent) { // create simple model for storing data // user's table data model //! [1] m_model = new CustomTableModel; //! [1] //! [2] // create table view and add model to it QTableView *tableView = new QTableView; tableView->setModel(m_model); tableView->setMinimumWidth(300); tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); m_model->setParent(tableView); //! [2] //! [3] QChart *chart = new QChart; chart->setAnimationOptions(QChart::AllAnimations); //! [3] // series 1 //! [4] //QBarSeries *series = new QBarSeries; QLineSeries* series = new QLineSeries; int first = 3; int count = 5; //QVBarModelMapper *mapper = new QVBarModelMapper(this); //mapper->setFirstBarSetColumn(1); //mapper->setLastBarSetColumn(4); QVXYModelMapper *mapper = new QVXYModelMapper(this); mapper->setXColumn(0); mapper->setYColumn(1); mapper->setSeries(series); mapper->setModel(m_model); chart->addSeries(series); QVXYModelMapper* mapper2 = new QVXYModelMapper(this); QLineSeries* series2 = new QLineSeries; mapper2->setXColumn(2); mapper2->setYColumn(3); mapper2->setSeries(series2); mapper2->setModel(m_model); chart->addSeries(series2); /*series->setBrush(QBrush(QColor(255, 0, 0))); mapper = new QVXYModelMapper(this); mapper->setXColumn(2); mapper->setYColumn(3); mapper->setSeries(series); mapper->setModel(m_model); chart->addSeries(series);*/ //! [4] //! [5] // for storing color hex from the series QString seriesColorHex = "#000000"; // get the color of the series and use it for showing the mapped area /* QList<QBarSet *> barsets = series->barSets(); for (int i = 0; i < barsets.count(); i++) { seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper(); m_model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count())); }*/ //! [5] //! [6] QStringList categories; categories << "April" << "May" << "June" << "July" << "August"; /*QBarCategoryAxis *axisX = new QBarCategoryAxis(); axisX->append(categories); chart->addAxis(axisX, Qt::AlignBottom); series->attachAxis(axisX); QValueAxis *axisY = new QValueAxis(); chart->addAxis(axisY, Qt::AlignLeft); series->attachAxis(axisY);*/ QValueAxis* axisX = new QValueAxis(); axisX->setRange(0, 100); axisX->setTickCount(10); QValueAxis* axisY = new QValueAxis(); axisY->setRange(0, 100); axisY->setTickCount(10); chart->setAxisX(axisX); chart->setAxisY(axisY); //! [6] //! [7] QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); chartView->setMinimumSize(640, 480); //! [7] //! [8] // create main layout QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(tableView, 1, 0); mainLayout->addWidget(chartView, 1, 1); mainLayout->setColumnStretch(1, 1); mainLayout->setColumnStretch(0, 0); setLayout(mainLayout); //! [8] }I'm looking at a custom table example.
Initialize data to 0 when creating a model and double-click in the table to change it. Table data is changed, but charts are not drawn.Replacing m_data through setdata works normally. But the chart doesn't change.
-
Hi,
Are you using this example ?
-
you're right. I modified this example a bit.
I am trying to insert data sequentially by modifying m_data containing data from the beginning, but the chart does not respond.https://forum.qt.io/topic/137543/update-problem-when-putting-data-into-qstandardmodel/3
Let's discuss this topic together