[PyQt] Pixel pushing performance

Roberto Alsina ralsina at kde.org
Thu May 8 02:59:33 BST 2008


On Wednesday 07 May 2008 20:16:25 Håkon Bertheussen wrote:
> Hi,
>
> I'm just getting started with PyQt, trying to implement a fairly complex
> widget in python. Unfortunately, it cannot easily be painted using
> higher level drawing primitives, meaning that I have to set individual
> pixels. I realize that this obviously has a lot of overhead compared to
> a C++ implementation, but if possible I'd like to stay in Python. Here's
> what I'm doing now:
>
>      def paintEvent(self, event):
>          canvas = QtGui.QImage(event.rect().size(),
>                                QtGui.QImage.Format_RGB32)
>          for i in xrange(0, canvas.height()):
>              for j in xrange(0, canvas.width()):
>                  canvas.setPixel(j, i, 123456)
>          painter = QtGui.QPainter()
>          painter.begin(self)
>          painter.fillRect(event.rect(), QtGui.QBrush(canvas))
>          painter.end()
>
> Obviously, in my real application the inner statement in the loop is a
> bit more complex, but even the above example is slow when the widget is
> fairly large (e.g. 300x300px). Is there a way to speed this up?

Try putting very little busywork in there instead of a setPixel (like, 
calculate a square root),and I bet that will slow enough to be noticeable, 
too.

You need to find a way not to *have* to do that, or you will need to do it 
outside python. 300x300 are 90K iterations. 90K anything takes a little 
while. 

   
-- 
 ("\''/").__..-''"`-. .         Roberto Alsina
 `9_ 9  )   `-. (    ).`-._.`)  KDE Developer (MFCH)
 (_Y_.)' ._   ) `._`.  " -.-'  http://lateral.netmanagers.com.ar 
  _..`-'_..-_/ /-'_.'     The 6,855th most popular site of Slovenia   
(l)-'' ((i).' ((!.'       according to alexa.com (27/5/2007) 
                    
Debugging is twice as hard as writing the code in the first place. 
Therefore, if you write the code as cleverly as possible, you are, 
by definition, not smart enough to debug it. --Brian W. Kernighan 



More information about the PyQt mailing list