Drawing QwtPlotCurve without deleting old data
Hello,
I am trying to plot (realtime) some lines for some sensor data without removing the older lines on the QwtPlot object from the past.
Code:
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
);
plotM->setMouseButton(Qt::NoButton);
In every 5 seconds I get new data from sensors and for each sensor I create a QwtPlotCurve then update my sensor-data with setSamples(points)
Code:
{
for(unsigned int i= 0; i< m_SensorNameList.count(); ++i)
{
m_PlotC
[m_SensorNameList.
at(i
)]->setRenderHint
(QwtPlotItem::RenderAntialiased,
true);
m_PlotC
[m_SensorNameList.
at(i
)]->setCurveAttribute
(QwtPlotCurve::Fitted);
}
m_PlotC[sensorId]->setSamples(points);
emit updatePlotSignal(m_PlotC);
}
Finally I update my plot over signal/slot
Code:
void mainGui
::updatePlotSlot(QMap<QString,
QwtPlotCurve*> plotC
) {
for(QMap<QString,
QwtPlotCurve*>
::iterator it
= plotC.
begin(); it
!= plotC.
end();
++it
) {
plotC[it.key()]->attach(m_Gui.guiQwtPlot);
m_Gui.guiQwtPlot->replot();
}
}
My problem is: it plots only the data, those I got in 5 sec and then the lines are replaced with the new data after 5 sec. What should I do, so that the lines will not be replaced and increased until I stop drawing?
May be second question: The drawn data is not centered to the QwtPlot-window, it overflows in +y-axis, if the data is higher then the set boundaries setAxisScale(QwtPlot::yLeft, -30.0, 30.0); How can I draw in the center of y-axis, so that the size of the QwtPlot does not matter.
Thank you
Re: Drawing QwtPlotCurve without deleting old data
Quote:
Originally Posted by
nasil122002
What should I do, so that the lines will not be replaced and increased until I stop drawing?
Have a look at QwtPlotDirectPainter.
The realtime example shows how to use it. The oscilloscope is probably much closer to your use case, but the source code requires some time to understand how it works.
Quote:
Originally Posted by
nasil122002
How can I draw in the center of y-axis, so that the size of the QwtPlot does not matter.
Not sure if I understand this one.
Uwe
Re: Drawing QwtPlotCurve without deleting old data
Quote:
Originally Posted by
Uwe
Not sure if I understand this one.
Uwe
Sorry for bad explanation. I found the solution by changing the yLeft- Axis scale from setAxisScale(QwtPlot::yLeft, -30.0, 30.0); to setAxisScale(QwtPlot::yLeft, -200.0, 200.0); Now the lines are aligned correctly
Re: Drawing QwtPlotCurve without deleting old data
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:
Code:
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);
The initialization of QwtPlotCurve: For my each sensor, I create QwtPlotCurve, QwtPlotDirectPainter and add curveData to the QwtPlotCurve
Code:
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());
}
}
}
After each 5 seconds, I get new data from sensors and for each sensor I append the QPolygonF points:
Code:
{
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);
}
}
update plot:
Code:
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();
}
}
In curveData class I use QPolygonF insteat of QPointF in append function:
Code:
class curveData: public QwtArraySeriesData<QPointF>
{
public:
...
{
d_samples+= point;
}
...
};
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.