Happy 2016!

I got a fun little challenge to kick off the new year:
I need to draw some text in a custom painter() method. The incoming text is of arbitrary length and needs to be wrapped like so:

painter.drawText(textbox, self._alignment|Qt.TextWordWrap, self._message)

All good so far. I now need to paint a background box behind the text to make it more readable inside the main UI. Since the drawText() method is taking care of wrapping the text, I can't draw the background box before drawing the text like so:

textbox = painter.drawText(textbox, self._alignment|Qt.TextWordWrap, self._message)
        painter.setBrush(QBrush(QColor(128,128,128)))
        painter.drawRect(textbox)

This adjusts the box the way I need it to, but of course the box is now drawn on top of the text and occluding it. Two solutions spring to mind and I was what people with more QT experience would do:
1 - just draw the text again on top of the box like so:
textbox = painter.drawText(textbox, self._alignment|Qt.TextWordWrap, self._message) # draw to get the bounding rectangle
        painter.setBrush(QBrush(QColor(128,128,128)))
        painter.drawRect(textbox)
        painter.setPen(Qt.white)
painter.drawText(textbox, self._alignment|Qt.TextWordWrap, self._message) # draw again on top of the box

2 - invert the text and use it as an alpha channel to stencil out the background box so when it is painted after the text, it won't occlude it.


Any opinions in terms of efficiency?

Cheers,
frank



--
ohufxLogo 50x50 <http://www.ohufx.com> *vfx compositing <http://ohufx.com/index.php/vfx-compositing> | *workflow customisation and consulting <http://ohufx.com/index.php/vfx-customising>* *

_______________________________________________
PySide mailing list
PySide@qt-project.org
http://lists.qt-project.org/mailman/listinfo/pyside

Reply via email to