Results 1 to 4 of 4

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

  1. #1
    Join Date
    Aug 2010
    Posts
    5

    Default QPainter slow, how to write into a buffer instead of repainting again and again?

    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/M...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:
    Qt Code:
    1. def paintEvent(self, event):
    2. paint = QtGui.QPainter()
    3. paint.begin(self)
    4.  
    5. paint.setPen(QtGui.QColor("white"))
    6. paint.setBrush(QtGui.QColor("black"))
    7.  
    8. paint.drawRect(0,0,800,50)
    9. paint.setPen(QtGui.QColor("yellow"))
    10. for i in range(0, 800):
    11. l=int(random.uniform(0,30))
    12. paint.drawLine(i, 48, i, l)
    13. paint.end()
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by CaCO3; 28th August 2010 at 00:53.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPainter slow, how to write into a buffer instead of repainting again and again?

    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:

    Qt Code:
    1. QPixmap myPixmap(100, 100);
    2. QPainter myPainter(&myPixmap);
    3. myPainter.drawLine(...);
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QPainter slow, how to write into a buffer instead of repainting again and again?

    ...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

  4. #4
    Join Date
    Aug 2010
    Posts
    5

    Default Re: QPainter slow, how to write into a buffer instead of repainting again and again?

    Hi all

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


    For others, here the minimal code:
    Qt Code:
    1. def CreateGraph()
    2. data.GraphBitMap = QtGui.QPixmap(500, 50) #create bitmap for graph
    3. GenerateGraph(data.GraphBitMap, QtGui) # draw graph
    4.  
    5.  
    6.  
    7. def GenerateGraph(self, QtGui):
    8. paint = QtGui.QPainter()
    9. paint.begin(self)
    10.  
    11. #drawwing data here
    12.  
    13. paint.end()
    14.  
    15.  
    16.  
    17. def paintEvent(self, event):
    18.  
    19. paint = QtGui.QPainter()
    20. paint.begin(self)
    21.  
    22. paint.drawPixmap(0, 0, data.GraphBitMap) #load graph from Bitmap
    23.  
    24. paint.end()
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. Replies: 3
    Last Post: 28th May 2010, 01:07
  2. Repainting a QGraphicsView
    By Luc4 in forum Qt Programming
    Replies: 8
    Last Post: 29th April 2010, 15:09
  3. Replies: 1
    Last Post: 24th January 2010, 12:15
  4. Very slow repainting ofPixmaps in QGraphicsView images
    By drexiya in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 19:13
  5. Replies: 13
    Last Post: 29th April 2009, 16:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.