[PyQt] QPainter and transparencies

548 views
Skip to first unread message

Manuel Macha

unread,
Sep 8, 2012, 10:35:05 PM9/8/12
to python_in...@googlegroups.com
I'm having a hard time figuring out how to best deal with transparencies using QPainter (PyQt 4.7 on Mac OSX)
I'm getting noisy artifacts in transparent areas of my icon which is painted using a custom QIconEngine class
http://pastebin.com/ES55EeQj

I'm trying to follow the advice in these articles where I explicitly set the pixelFormat in a pixmap and then fill it with transparency, however I can't get rid of the artifacts.
http://techbase.kde.org/Development/Tutorials/Graphics/Performance
http://www.informit.com/articles/article.aspx?p=1174421&seqNum=3

Any help would be greatly appreciated.

Justin Israel

unread,
Sep 9, 2012, 3:04:58 PM9/9/12
to python_in...@googlegroups.com
I think the one line that may fix it would be this:

painter.setCompositionMode(painter.CompositionMode_Source)

It tells the painter to just copy the new pixel, without considering the destination pixels alpha. It may related to how the drawEllipse handles the area outside the shape, over the previously drawn transparent pixels

I didn't need to do it with a pixmap or image first, but then again I am not sure what you will be doing in this paint method beyond this test example:

def paint(self, painter, rect, mode, state):

painter.setRenderHint(painter.Antialiasing)
painter.setCompositionMode(painter.CompositionMode_Source)

painter.fillRect(rect, QtCore.Qt.transparent)

painter.setPen(QtGui.QPen(QtCore.Qt.red))
painter.setBrush(QtGui.QBrush(QtCore.Qt.red))        
painter.drawEllipse(rect)  



Manuel Macha

unread,
Sep 10, 2012, 12:06:03 AM9/10/12
to python_in...@googlegroups.com
Hi Justen,
thanks for your reply.
I've tested your code at work on a CentOS 5 box and unfortunately it doesn't work as expected.
The artifacts are gone, however the icon is coloured black where I'd expect it to be transparent.
Not sure if it's worth to further pursue this for what I was planning to do with it.
Thanks for your help,
Manuel

Justin Israel

unread,
Sep 10, 2012, 12:09:54 AM9/10/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
I have only tried it on OSX. Maybe it wasn't a platform independent solution. Did it not work, even when using your original QImage approach?


Manuel Macha

unread,
Sep 10, 2012, 12:45:55 AM9/10/12
to python_in...@googlegroups.com
unfortunately not :-(
Reply all
Reply to author
Forward
0 new messages