QLineSeries添加数据后如何update/redrawQChart?
How to update/redraw QChart after data is added to QLineSeries?
我正在生成一些我想使用 QChart 和朋友绘制图表的数据。这是我第一次使用 QChart
,所以基本上我所做的就是复制 QLineSeries Example 并根据我的需要对其进行修改。我当前的代码如下所示:
quint64 last=0;
quint64 *lastp=&last;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, series( nullptr )
{
ui->setupUi(this);
QChart *chart = new QChart();
series=new QLineSeries(chart);
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
setCentralWidget(chartView);
GeneticTask *gTask = new GeneticTask();
connect(gTask, &GeneticTask::point, this, [=](QPointF pt) {
// New point added to series
*series<<pt;
// Limit updates to once per second
quint64 now=QDateTime::currentMSecsSinceEpoch();
if(now-(*lastp)>1000) {
qDebug()<<"UPDATE";
// [A] WHAT TO PUT HERE TO HAVE CHART REDRAW WITH NEW DATA?
*lastp=now;
}
}
);
QThreadPool::globalInstance()->start(gTask);
}
当我 运行 此代码时,我希望我的新数据显示在图表中,但事实并非如此,所以我的问题是:我怎样才能将图表更新为show the new data? 换句话说,我应该在注释为 [A]
?
的代码中添加什么
使用运算符 <<
或 append
方法将值附加到 QLineSeries
应重新绘制图形。如果由于某种原因没有发生,您可以尝试在 QChartView
.
上调用 repaint
方法
下面是一些代码,一旦添加了数据,上限为每秒最多一次,就会使数据居中:
// 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;
}
}
);
对上面的答案稍作更正。 Qt 文档说:
void QWidget::repaint()
Repaints the widget directly by calling
paintEvent() immediately, unless updates are disabled or the widget is
hidden. We suggest only using repaint() if you need an immediate
repaint, for example during animation. In almost all circumstances
update() is better, as it permits Qt to optimize for speed and
minimize flicker.
Warning: If you call repaint() in a function which
may itself be called from paintEvent(), you may get infinite
recursion. The update() function never causes recursion.
结果,QChartView::update()
适合我。
我正在生成一些我想使用 QChart 和朋友绘制图表的数据。这是我第一次使用 QChart
,所以基本上我所做的就是复制 QLineSeries Example 并根据我的需要对其进行修改。我当前的代码如下所示:
quint64 last=0;
quint64 *lastp=&last;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, series( nullptr )
{
ui->setupUi(this);
QChart *chart = new QChart();
series=new QLineSeries(chart);
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
setCentralWidget(chartView);
GeneticTask *gTask = new GeneticTask();
connect(gTask, &GeneticTask::point, this, [=](QPointF pt) {
// New point added to series
*series<<pt;
// Limit updates to once per second
quint64 now=QDateTime::currentMSecsSinceEpoch();
if(now-(*lastp)>1000) {
qDebug()<<"UPDATE";
// [A] WHAT TO PUT HERE TO HAVE CHART REDRAW WITH NEW DATA?
*lastp=now;
}
}
);
QThreadPool::globalInstance()->start(gTask);
}
当我 运行 此代码时,我希望我的新数据显示在图表中,但事实并非如此,所以我的问题是:我怎样才能将图表更新为show the new data? 换句话说,我应该在注释为 [A]
?
使用运算符 <<
或 append
方法将值附加到 QLineSeries
应重新绘制图形。如果由于某种原因没有发生,您可以尝试在 QChartView
.
repaint
方法
下面是一些代码,一旦添加了数据,上限为每秒最多一次,就会使数据居中:
// 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;
}
}
);
对上面的答案稍作更正。 Qt 文档说:
void QWidget::repaint()
Repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the widget is hidden. We suggest only using repaint() if you need an immediate repaint, for example during animation. In almost all circumstances update() is better, as it permits Qt to optimize for speed and minimize flicker.
Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion.
结果,QChartView::update()
适合我。