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. Update problem when putting data into QStandardModel
Forum Updated to NodeBB v4.3 + New Features

Update problem when putting data into QStandardModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 175 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.
  • I Offline
    I Offline
    IknowQT
    wrote on 29 Jun 2022, 08:15 last edited by
    #1

    The chart output result is different when you put data sequentially in StandardItemModel and when you put data all at once.

    When multiple data are put in at once, the chart is drawn well according to the model data. By the way, if you put them one by one sequentially, the chart is not drawn. I'd like to know if it's an update issue, another issue, or a bug.

    TableWidget::TableWidget(QWidget *parent)
        : QWidget(parent)
    {
        // create simple model for storing data
        // user's table data model
        //! [1]
        //m_model = new CustomTableModel;
        // 
        // 
        //! [1]
    
    
        QStandardItemModel* m_model = new QStandardItemModel();
    
        QStringList categories;
        categories << "X" << "Y1" << "Y2";
        m_model->setHorizontalHeaderLabels(categories);
    
        /*categories.clear();
        categories << "10" << "20" << "30" << "40" << "50" << "60" << "70" << "80" << "90" << "100";
        m_model->setVerticalHeaderLabels(categories);*/
        
    
        int a = m_model->columnCount();
        int b = m_model->rowCount();
        nMin = 999999, nMax = -999999;
        
        /*QList<QStandardItem*> items;
        items.append(new QStandardItem(QString::number(10)));
        items.append(new QStandardItem(QString::number(20)));
        items.append(new QStandardItem(QString::number(30)));
        items.append(new QStandardItem(QString::number(40)));
        items.append(new QStandardItem(QString::number(50)));
        items.append(new QStandardItem(QString::number(60)));
        items.append(new QStandardItem(QString::number(70)));
        items.append(new QStandardItem(QString::number(80)));
        items.append(new QStandardItem(QString::number(90)));
        items.append(new QStandardItem(QString::number(100)));
        m_model->insertColumn(0, items);*/
    
        m_model->setItem(0, 0, new QStandardItem(QString::number(10)));
        m_model->setItem(1, 0, new QStandardItem(QString::number(20)));
        m_model->setItem(2, 0, new QStandardItem(QString::number(30)));
        m_model->setItem(3, 0, new QStandardItem(QString::number(40)));
        m_model->setItem(4, 0, new QStandardItem(QString::number(50)));
        m_model->setItem(5, 0, new QStandardItem(QString::number(60)));
        m_model->setItem(6, 0, new QStandardItem(QString::number(70)));
        m_model->setItem(7, 0, new QStandardItem(QString::number(80)));
        m_model->setItem(8, 0, new QStandardItem(QString::number(90)));
        m_model->setItem(9, 0, new QStandardItem(QString::number(100)));
    
    
        //for (int j = 0; j < 10; j++)
        //{
        //    QList<QStandardItem*> items;
    
        //    /*items.append(new QStandardItem(QString::number((j+1)*10)));
        //    items.append(new QStandardItem(QString::number(QRandomGenerator::global()->bounded(100))));
        //    items.append(new QStandardItem(QString::number(QRandomGenerator::global()->bounded(100))));*/
    
    
        //    items.append(new QStandardItem(QString::number((j + 1) * 10)));
        //    
        //    int nRan = QRandomGenerator::global()->bounded(100);
        //    items.append(new QStandardItem(QString::number(nRan)));
    
        //    if (nMin > nRan)
        //        nMin = nRan;
    
        //    if (nMax < nRan)
        //        nMax = nRan;
        //   
        //    nRan = QRandomGenerator::global()->bounded(100);
        //    items.append(new QStandardItem(QString::number(nRan)));
    
        //    if (nMin > nRan)
        //        nMin = nRan;
    
        //    if (nMax < nRan)
        //        nMax = nRan;
    
        //    m_model->appendRow(items);
        //}
    
       /* for (int n = 0; n < m_model->rowCount(); n++)
        {
            for (int k = 0; k < m_model->columnCount(); k++)
            {
    
            }
        }*/
        
    
    
    
    
        //! [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]
        this->chart = new QChart();
        this->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);
        this->chart->addSeries(series);
    
        QVXYModelMapper* mapper2 = new QVXYModelMapper(this);
        QLineSeries* series2 = new QLineSeries;
        mapper2->setXColumn(0);
        mapper2->setYColumn(2);
        mapper2->setSeries(series2);
        mapper2->setModel(m_model);
        this->chart->addSeries(series2);
    
       /* QVXYModelMapper* mapper = new QHXYModelMapper(this);
        mapper->setXRow(0);
        mapper->setYRow(1);
        mapper->setSeries(series);
        mapper->setModel(m_model);
        chart->addSeries(series);
    
        QVXYModelMapper* mapper2 = new QHXYModelMapper(this);
        QLineSeries* series2 = new QLineSeries;
        mapper2->setXRow(0);
        mapper2->setYRow(2);
        mapper2->setSeries(series2);
        mapper2->setModel(m_model);
        chart->addSeries(series2);*/
    
    
        this->chart->createDefaultAxes();
        QValueAxis* axisX = new QValueAxis();
        axisX->setRange(10, 100);
        //axisX->setTickCount(9);
    
        QValueAxis* axisY = new QValueAxis();
        axisY->setRange(nMin, nMax);
        //axisY->setTickCount(9);
    
        this->chart->setAxisX(axisX);
        this->chart->setAxisY(axisY);
    
    
        /*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]
    
       
    
        /*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);*/
    
        
        //! [6]
    
        //! [7]
        QChartView *chartView = new QChartView(this->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]
        //! 
        //! 
       //QStandardItem *item
        this->connect(m_model, QOverload<QStandardItem*>::of(&QStandardItemModel::itemChanged), [=](QStandardItem* item)
            {
                qDebug() << "*" << item->data().toInt();
            });
    
        this->connect(tableView, QOverload<const QModelIndex&>::of(&QAbstractItemView::clicked), [=](const QModelIndex& index)
            {
                qDebug() << "**" << index << " | " << m_model->index(index.row(), index.column()).data().toString();
            });
    
    
        this->nRow = 0;
        this->nCol = 0;
        m_pTimer = new QTimer();
    
        this->connect(this->m_pTimer, &QTimer::timeout, [=]()
            {
                qDebug() << "***" << m_model->columnCount();
                qDebug() << "****" << m_model->rowCount();
    
                if (++this->nCol >= m_model->columnCount())
                {
                    this->nRow++;
                    this->nCol = 1;
                }           
    
                if (nRow >= m_model->rowCount())
                {
                    m_pTimer->stop();
    
                    QValueAxis* axisx = qobject_cast<QValueAxis*>(this->chart->axes(Qt::Horizontal).first());
                    axisx->setRange(10, 100);
    
                    QValueAxis* axisy = qobject_cast<QValueAxis*>(this->chart->axes(Qt::Vertical).first());
                    axisy->setRange(this->nMin, this->nMax);
    
    
                    return;
                }
    
                int nRan = QRandomGenerator::global()->bounded(100);
    
                if (this->nMin > nRan)
    				this->nMin = nRan;
    
    			if (this->nMax < nRan)
    				this->nMax = nRan;
    
                m_model->setItem(nRow, nCol, new QStandardItem(QString::number(nRan)));
            });
    
        m_pTimer->start(500);
    
    
        
    }
    
    1 Reply Last reply
    0
    • kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on 29 Jun 2022, 10:48 last edited by
      #2

      Can you make it a bit easier for us by providing a minimal, but compilable example?

      Director R&D, The Qt Company

      I 1 Reply Last reply 29 Jun 2022, 23:06
      0
      • kkoehneK kkoehne
        29 Jun 2022, 10:48

        Can you make it a bit easier for us by providing a minimal, but compilable example?

        I Offline
        I Offline
        IknowQT
        wrote on 29 Jun 2022, 23:06 last edited by IknowQT
        #3

        @kkoehne

        https://github.com/HaonPAPA/MyRepository/blob/main/barmodelmapper.7z

        I've posted an example that I've modified.

        If you put data sequentially, it feels like the coordinate system of the chart changes.
        I set the range of the Y-axis as 0-100, but it seems to change between 0.5-0.5.

        1 Reply Last reply
        0

        1/3

        29 Jun 2022, 08:15

        • Login

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