且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据添加到 QLineSeries 后如何更新/重绘 QChart?

更新时间:2023-02-05 18:29:11

使用运算符 <<appendQLineSeries/code> 方法应该重新绘制图形.如果由于某种原因没有发生,您可以尝试调用 QChartView 上的 repaint 方法.

Appending a value to QLineSeries using the operator << or the append method should repaint the graph. If it does not happen form some reason, you could trying calling the repaint method on the QChartView.

以下是一些代码,在添加数据后将数据居中,上限为每秒一次:

Here is some code that will center the data once it is added with a cap of at most once per second:

// Global or class scope or
qreal max=-10000000000;
qreal min=-max;
qreal *maxp=&max;
qreal *minp=&min;

// Same scope as before
connect(gTask, &GeneticTask::point, this, [=](QPointF pt) {
        if(pt.y()>*maxp) {
            *maxp=pt.y();
        }
        if(pt.y()<*minp) {
            *minp=pt.y();
        }
        *series<<pt;
        quint64 now=QDateTime::currentMSecsSinceEpoch();
        if(now-(*lastp)>1000) {
            qDebug()<<"UPDATE";
            chart->axisX()->setRange(0,series->count());
            chart->axisY()->setRange(*minp,*maxp);

            *lastp=now;
        }
    }
);