How to repaint a QChart

后端 未结 2 861
离开以前
离开以前 2021-02-10 01:46

I\'d like to know how to repaint a QChart after I append new points to the QLineSeries added to it. The goal is to use this for displa

2条回答
  •  执笔经年
    2021-02-10 02:15

    Apparently QCharts doesn't need repaint(). Appending new points to the series seems to be enough. I wasn't seeing the data because I hadn't set the axis for the char and also because values weren't properly calculated.

    Corrected code:

    Header:

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
        ui->setupUi(this);
    
        series = new QLineSeries();
    
        chart = new QChart();    
        chart->addSeries(series);
    
        chart->createDefaultAxes(); // Preparing the axis
        chart->axisX()->setRange(0,10); 
        chart->axisY()->setRange(0,10); 
    
        // Same formatting
        chart->setBackgroundVisible(false);
        chart->setMargins(QMargins(0,0,0,0));
        chart->layout()->setContentsMargins(0,0,0,0);
        chart->legend()->hide();
        chart->setPlotAreaBackgroundBrush(QBrush(Qt::black));
        chart->setPlotAreaBackgroundVisible(true);
        chartView = new QChartView(chart);
        ui->gridLayout->addWidget(chartView);
    }
    

    And the pushButton code, casting cnt to double before calculation.

    void MainWindow::on_pB_Start_clicked(){
        double val = 3*(qSin((double)cnt*2)+2);
        series->append(cnt,val); // Enough to trigger repaint!
        cnt++;
    }
    

提交回复
热议问题