我有一个线程,每秒从外部设备读取一次数据。我想绘制一个使用qwt显示此数据的图表,但我无法绘制任何内容。
我已经在我的对话框中添加了一个插槽,该插槽将传递要绘制的数据,并在UI上添加一个标签供其绘制,但我现在所有的内容都出现在左上角。插槽代码如下所示:
void Dialog::updateGraph(double *pdXData, double *pdYData, unsigned int nCount)
{
QwtPlotCurve curve;
curve.setPen( Qt::darkBlue );
curve.setStyle( QwtPlotCurve::Lines );
curve.setRenderHint( QwtPlotItem::RenderAntialiased );
curve.setRawSamples(pdXData, pdYData, nCount);
QwtScaleMap xMap;
QwtScaleMap yMap;
xMap.setScaleInterval(0, nCount - 1);
yMap.setScaleInterval(0, 50); // TODO - max
QPixmap pixmap(300, 200);
QPainter painter(&pixmap);
curve.draw(&painter, xMap, yMap, ui->graph->rect());
ui->graph->setPixmap(pixmap);
ui->graph->show();
}
更新:如果我用图片创建像素图然后绘制,但仍然没有图形。
答案 0 :(得分:0)
我在Qt Creator中使用Qwt解决了我的问题(参见Can't get qwt_designer_plugin to load in Qt Creator),所以我能够将位图更改为QwtPlot。然后我发现我仍然没有得到图表,直到我让QwtPlotCurve成为班级成员;我认为它存储了绘制到图形的数据,因此在重绘期间需要存在。最后,我修复了一个内存泄漏,发现我的数组双打并没有被Qt克隆,所以我把它改成了QVector。
工作代码如下所示:
void Dialog::updateGraph(QVector<QPointF> data)
{
_curve.setPen( Qt::darkBlue );
_curve.setStyle( QwtPlotCurve::Lines );
_curve.setRenderHint( QwtPlotItem::RenderAntialiased );
_curve.setSamples(data);
ui->qwtPlot->setAxisScale(QwtPlot::xBottom, 0.0, data.size() - 1, 1);
ui->qwtPlot->setAxisScale(QwtPlot::yLeft, 0.0, 50.0, 10.0); // TODO - max
_curve.attach(ui->qwtPlot);
ui->qwtPlot->replot();
}
答案 1 :(得分:-1)
您只能从主线程
更新graphique