Ok I have adapted my code above to the QwtPlotDirectPainter real-time example.
By the initialization of QwtPlot I added additionally canvas()->setAttribute() and setAutoReplot() functions:
m_Gui.guiQwtPlot->setTitle(title);
m_Gui.
guiQwtPlot->setCanvasBackground
(* new QBrush(Qt
::black));
m_Gui.
guiQwtPlot->setAxisScale
(QwtPlot::yLeft,
-30.0,
30.0);
m_Gui.
guiQwtPlot->insertLegend
(new QwtLegend());
m_Gui.guiQwtPlot->legend()->setFont(fontNormal);
m_Gui.guiQwtPlot->setFont(fontNormal);
m_Gui.
guiQwtPlot->setAxisFont
(QwtPlot::yLeft, fontNormal
);
m_Gui.
guiQwtPlot->setAxisFont
(QwtPlot::xBottom, fontNormal
);
m_Gui.
guiQwtPlot->setAxisScaleDraw
(QwtPlot::xBottom,
new TimeScaleDraw
(QTime::currentTime()));
m_Gui.
guiQwtPlot->setAxisLabelRotation
(QwtPlot::xBottom,
0.0);
m_Gui.
guiQwtPlot->setAxisLabelAlignment
(QwtPlot::xBottom,Qt
::AlignLeft|Qt
::AlignBottom);
m_Gui.
guiQwtPlot->setAxisTitle
(QwtPlot::xBottom, axisTitleBottom
);
m_Gui.
guiQwtPlot->setAxisTitle
(QwtPlot::yLeft, axisTitleLeft
);
m_Gui.guiQwtPlot->canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
m_Gui.guiQwtPlot->setAutoReplot(false);
plotM->setMouseButton(Qt::NoButton);
void guiMain::createPlot(QStringList m_SensorNameList)
m_Gui.guiQwtPlot->setTitle(title);
m_Gui.guiQwtPlot->setCanvasBackground(* new QBrush(Qt::black));
m_Gui.guiQwtPlot->setAxisScale(QwtPlot::yLeft, -30.0, 30.0);
m_Gui.guiQwtPlot->insertLegend(new QwtLegend());
m_Gui.guiQwtPlot->legend()->setFont(fontNormal);
m_Gui.guiQwtPlot->setFont(fontNormal);
m_Gui.guiQwtPlot->setAxisFont(QwtPlot::yLeft, fontNormal);
m_Gui.guiQwtPlot->setAxisFont(QwtPlot::xBottom, fontNormal);
m_Gui.guiQwtPlot->setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(QTime::currentTime()));
m_Gui.guiQwtPlot->setAxisLabelRotation(QwtPlot::xBottom, 0.0);
m_Gui.guiQwtPlot->setAxisLabelAlignment(QwtPlot::xBottom,Qt::AlignLeft|Qt::AlignBottom);
m_Gui.guiQwtPlot->setAxisTitle(QwtPlot::xBottom, axisTitleBottom);
m_Gui.guiQwtPlot->setAxisTitle(QwtPlot::yLeft, axisTitleLeft);
m_Gui.guiQwtPlot->canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
m_Gui.guiQwtPlot->setAutoReplot(false);
QwtPlotMagnifier *plotM= new QwtPlotMagnifier(m_Gui.guiQwtPlot->canvas());
plotM->setMouseButton(Qt::NoButton);
QwtPlotPanner *plotP= new QwtPlotPanner(m_Gui.guiQwtPlot->canvas());
To copy to clipboard, switch view to plain text mode
The initialization of QwtPlotCurve: For my each sensor, I create QwtPlotCurve, QwtPlotDirectPainter and add curveData to the QwtPlotCurve
void plotting
::createPlotCurve(QStringList m_SensorNameList
) {
QMap<QString, QwtPlotDirectPainter*> m_DirectPainter;
if(!m_SensorNameList.isEmpty())
{
for(unsigned int i= 0; i< m_SensorNameList.count(); ++i)
{
m_PlotC
[m_SensorNameList.
at(i
)]->setPen
(QPen(QColor(m_ColorList
[i
]),
1, Qt
::SolidLine));
m_PlotC
[m_SensorNameList.
at(i
)]->setRenderHint
(QwtPlotItem::RenderAntialiased,
true);
m_PlotC
[m_SensorNameList.
at(i
)]->setCurveAttribute
(QwtPlotCurve::Fitted);
m_DirectPainter[m_SensorNameList.at(i)]= new QwtPlotDirectPainter();
m_PlotC[m_SensorNameList.at(i)]->setData(new curveData());
}
}
}
void plotting::createPlotCurve(QStringList m_SensorNameList)
{
QMap<QString, QwtPlotCurve*> m_PlotC;
QMap<QString, QwtPlotDirectPainter*> m_DirectPainter;
if(!m_SensorNameList.isEmpty())
{
for(unsigned int i= 0; i< m_SensorNameList.count(); ++i)
{
m_PlotC[m_SensorNameList.at(i)]= new QwtPlotCurve();
m_PlotC[m_SensorNameList.at(i)]->setPen(QPen(QColor(m_ColorList[i]), 1, Qt::SolidLine));
m_PlotC[m_SensorNameList.at(i)]->setRenderHint(QwtPlotItem::RenderAntialiased, true);
m_PlotC[m_SensorNameList.at(i)]->setCurveAttribute(QwtPlotCurve::Fitted);
m_DirectPainter[m_SensorNameList.at(i)]= new QwtPlotDirectPainter();
m_PlotC[m_SensorNameList.at(i)]->setData(new curveData());
}
}
}
To copy to clipboard, switch view to plain text mode
After each 5 seconds, I get new data from sensors and for each sensor I append the QPolygonF points:
{
curveData *data= static_cast<curveData *>(m_PlotC[sensorId]->data());
data->clear();
if(m_SensorNameList.contains(sensorId))
{
//m_PlotC[sensorId]->setSamples(points);
//curveData *data= static_cast<curveData *>(m_PlotC[sensorId]->data());
data->append(points);
m_DirectPainter[sensorId]->drawSeries(m_PlotC[sensorId], data->size()-100, data->size()-1);
emit updatePlotSignal(m_PlotC);
}
}
void plotting::updateDataSlot(QString sensorId, QPolygonF points)
{
curveData *data= static_cast<curveData *>(m_PlotC[sensorId]->data());
data->clear();
if(m_SensorNameList.contains(sensorId))
{
//m_PlotC[sensorId]->setSamples(points);
//curveData *data= static_cast<curveData *>(m_PlotC[sensorId]->data());
data->append(points);
m_DirectPainter[sensorId]->drawSeries(m_PlotC[sensorId], data->size()-100, data->size()-1);
emit updatePlotSignal(m_PlotC);
}
}
To copy to clipboard, switch view to plain text mode
update plot:
void guiMain
::updatePlotSlot(QMap<QString,
QwtPlotCurve*> plotC
) {
m_Gui.guiQwtPlot->repaint();
for(QMap<QString,
QwtPlotCurve*>
::iterator it
= plotC.
begin(); it
!= plotC.
end();
++it
) {
plotC[it.key()]->attach(m_Gui.guiQwtPlot);
m_Gui.guiQwtPlot->replot();
}
}
void guiMain::updatePlotSlot(QMap<QString, QwtPlotCurve*> plotC)
{
m_Gui.guiQwtPlot->repaint();
for(QMap<QString, QwtPlotCurve*>::iterator it= plotC.begin(); it!= plotC.end(); ++it)
{
plotC[it.key()]->attach(m_Gui.guiQwtPlot);
m_Gui.guiQwtPlot->replot();
}
}
To copy to clipboard, switch view to plain text mode
In curveData class I use QPolygonF insteat of QPointF in append function:
class curveData: public QwtArraySeriesData<QPointF>
{
public:
...
{
d_samples+= point;
}
...
};
class curveData: public QwtArraySeriesData<QPointF>
{
public:
...
inline void append(const QPolygonF &point)
{
d_samples+= point;
}
...
};
To copy to clipboard, switch view to plain text mode
But somehow ,there is no difference with the previous plot. The lines are not appended and deleted from the scene after 5 sec. each time. If I comment out the m_Gui.guiQwtPlot->repaint(); then the lines are not cleared from the current plot but overlapped.
Bookmarks