Search code examples
htmlcssqttransparencyqpixmap

Drawing html with transparent background


I'm currently trying to draw an HTML formatted text of a QTextEdit into a QPixmap with a transparent background, to use it as an overlay for another widget.

My first attempt was, to use the QTextDocument of the QTextEdit and use drawContents() to draw it to a QPixmap.

As a second attempt, I passed the HTML to a QStaticText and painted this to my QPixmap, using a QPainter.

Both approaches paint the final text as expected, however, the background of the QPixmap is always grey. Also setting the background-color of the QTextEdit, inside the StyleSheet, to transparent does no change.

Is there any way, that I can make the background transparent?


Solution

  • Apparently, after some more research, I found the solution.

    My QPixmap was not transparent at all, even before rendering the text. I came across this blog post, in which the composition mode of the QPainter is set to CompositionMode_Source using a call to setCompositionMode().