[PySide] drawing text in dynamic box

Frank Rueter | OHUfx frank at ohufx.com
Mon Jan 4 08:15:12 CET 2016


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>* *

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160104/c7dc1ad0/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ohufxLogo_50x50.png
Type: image/png
Size: 2666 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160104/c7dc1ad0/attachment.png>


More information about the PySide mailing list