PDA

View Full Version : QPainter slow, how to write into a buffer instead of repainting again and again?



CaCO3
28th August 2010, 00:49
Hi all

I am writing a python application with PyQT for MAEMO 5 (N900).

I have to draw a graph with a lot of lines (see http://maemo.org/downloads/product/Maemo5/sleepanalyser/)

It works fine, how ever now I want to put the draw into a scroll area.
It works, however it is very slow when I scroll the area, as it is redrawing it all the time.
Is there a way I can store the drawing into a pixmap so it takes it from there?
It is a static drawing and does actually only have to be drawn once when I load the widget.
I searched the web all over but could not find a solution, mayby I search at the wrong corner.

A working example:


def paintEvent(self, event):
paint = QtGui.QPainter()
paint.begin(self)

paint.setPen(QtGui.QColor("white"))
paint.setBrush(QtGui.QColor("black"))

paint.drawRect(0,0,800,50)
paint.setPen(QtGui.QColor("yellow"))
for i in range(0, 800):
l=int(random.uniform(0,30))
paint.drawLine(i, 48, i, l)
paint.end()

I guess I have to use something like QImage or QBitmap, but I dont know how.
All I want is to draw it once and buffer it somewhere, so it can be taken from there every time paintEvent gets called.
Thank you for your advice!

tbscope
28th August 2010, 07:01
You can use a QPainter to draw on a pixmap for example.
I don't know how to do this in python, but in C++ it's done like this:


QPixmap myPixmap(100, 100);
QPainter myPainter(&myPixmap);
myPainter.drawLine(...);

This might be interesting too:
http://techbase.kde.org/Development/Tutorials/Graphics/Performance

Lykurg
28th August 2010, 07:20
...and with using QPixmap, QPixmapCache can be of interest too. See also a nice article here: http://doc.qt.nokia.com/qq/qq12-qpixmapcache.html

CaCO3
28th August 2010, 13:42
Hi all

Thank you very much for your tipps.
With the mentioned links, I now got it working.


For others, here the minimal code:



def CreateGraph()
data.GraphBitMap = QtGui.QPixmap(500, 50) #create bitmap for graph
GenerateGraph(data.GraphBitMap, QtGui) # draw graph



def GenerateGraph(self, QtGui):
paint = QtGui.QPainter()
paint.begin(self)

#drawwing data here

paint.end()



def paintEvent(self, event):

paint = QtGui.QPainter()
paint.begin(self)

paint.drawPixmap(0, 0, data.GraphBitMap) #load graph from Bitmap

paint.end()

I generate the pitmap, whenever it changes its content and store it in a cache.
Now, when ever paintEvent gets called, i just reload the bitmap, instead of creating it again.

It works very well and seems to use almost no CPU.