Reputation: 115
I have a custom QML component to plot graphs derived from QQuickPaintedItem
. In the paint method I traverse through a list of coordinates and plot them. My paint()
is as follows
QRectF bounds = boundingRect();
float w = bounds.width();
float h = bounds.height();
float dx = w / listSize;
for(int i = 0; i < coordinatesList.size() - 1; ++i)
{
QPointF m_p1(dx*i, (h/2)-(coordinatesList.at(i)*(h/4)));
QPointF m_p2(dx*(i+1), (h/2)-(coordinatesList.at(i+1)*(h/4)));
painter->drawLine(m_p1, m_p2);
}
Is there any way to retain the previously plotted points so that I don't have to repaint the entire list of points but only specific ones?
Upvotes: 0
Views: 663
Reputation: 49289
As the first answer suggests, caching is the key, however, using a by default this will use the raster painter backend which is not optimal, since it will involve costly ram to GPU ram transfers. Also, while the raster painter offers better quality, it is usually slower than the GL backend.QPixmap
If efficiency is key, then the correct approach would be to use a QOpenGLFramebufferObject
, draw on it with QPainter
and QOpenGLPaintDevice
, and in QQuickPaintedItem::paint()
use native rendering to draw the FBO on a QQuickItem
that uses an FBO target itself. This will give you better painting performance and avoid the overhead of ram to vram transfers.
Also, when dealing with plot components, it is a good idea to compartmentalize the drawing, for example stuff like the grid or scales should be separate components, since they don't need constant redrawing, and only draw the actual waveform and compose it on top.
Upvotes: 1
Reputation: 12854
I can't now test it but you can draw all the points on a QPixmap
m_pixmap.fill();
QPainter buffer(&m_pixmap);
....
buffer->drawLine(...);
...
and then paint saved pixmap on the Canvas
.
painter->drawPixmap(0, 0, m_pixmap);
At next paint request you just paint the saved QPixmap
. The QPixmap
itself have to be repaint only if plot changes.
Upvotes: 2