[PyQt] Faster transparency from a QImage?

Matt Newell newellm at blur.com
Tue Jun 17 22:59:17 BST 2008

On Tuesday 17 June 2008 13:17:00 Jeiel Aranal wrote:
> Is there a faster way to draw transparency on a QWidget from a QImage
> other than converting the QImage to a pixmap and setting that as the
> mask? I'm trying to have a 3d window draw into PyQt, the window output
> is going in fast enough but getting the alpha of the 3d window into
> the mask is being way too slow for my purpose. My paint event:
>     def paintEvent(self,  event):
>         if self.pandaTexture.mightHaveRamImage():
>             self.pandaTexture.setFormat(Texture.FRgba32)
>             #print "Should draw yes?"
>             data = self.pandaTexture.getRamImage().getData()
>             img = QtGui.QImage(data, self.pandaTexture.getXSize(),
> self.pandaTexture.getYSize(), QtGui.QImage.Format_ARGB32).mirrored()
>             self.paintSurface.begin(self)
>             self.paintSurface.drawPixmap(0, 0, self.desktopBg)
>             self.paintSurface.drawImage(0, 0, img)
>             self.paintSurface.end()
>             pixmap = QtGui.QPixmap.fromImage(img)
>             self.setMask(pixmap.mask())

I think QWidget.setMask will always be slow.  I think the only way to 
accomplish a changing mask is to use a visual that supports a true alpha 
channel, instead of using setMask.

You will also need to avoid so many copies of the data depending on how fast 
you need it.  The code above has at least 3 copies, possibly 5, plus the 
pixmap.mask and QWidget.setMask calls.  This is probably fine if the image is 
small, but will be prohibitively slow if the image is large.

The details of doing this will vary somewhat depending on what platform you 
are using.


More information about the PyQt mailing list