View Full Version : Create QPixmap with alpha channel and not premultiplied color channels

13th April 2011, 02:13
Hi I posted this before on stackoverflow and got no responses.

I would like to create a QPixmap to draw on using a QPainter. The QPixmap should support transparency without using premultiplied color channels.

Currently I do this by creating a QPixmap with the desired dimensions and filling it with a QColor that has been set to zero for each channel (including alpha).

tex = QtGui.QPixmap(width, height)
c = QtGui.QColor(0)

This adds transparency to the QPixmap. However, if I draw to the QPixmap using a QPainter, the drawn color values are premultiplied by the alpha value of the source. I don't want this because the QPixmap is later used as a texture in a QGLWidget and upon rendering the alpha channel of the QPixmap (now the alpha of the source that was drawn using the QPainter) is again multiplied against the color channels, so that the alpha is multiplied twice.

If I use a QImage with format QtGui.QImage.Format_ARGB32 in place of the QPixmap, then the color channels are not premultiplied and the alpha is applied only once. However this is too slow during rendering. I have tried to draw on QImages with the above format and then convert to QPixmaps, but got the same result (premultiplied color channels again being multiplied by the alpha channel). The Qt docs say,

Depending on the system, QPixmap is stored using a RGB32 or a premultiplied alpha format. If the image has an alpha channel, and if the system allows, the preferred format is premultiplied alpha.

I am using X (Linux). Is there any way to force a QPixmap to not premultiply the color channels when that QPixmap has an alpha channel?

28th April 2011, 03:13
I ended up using QImages and optimizing my code by minimizing the number of QGLWidget::bindTextures I was calling. I still have not received a satisfactory answer about how premultiplied QPixmaps can be used as semi-transparent textures, but I'm satisfied with my program performance and won't be checking this thread anymore.