[Qt-interest] Paint a QTextDocument with a Custom Pen

Josiah Bryan jbryan at productiveconcepts.com
Sat Sep 12 06:23:35 CEST 2009


Evening, All -

I'd like to draw a QTextDocument in a QGraphicsItem using a custom pen 
(e.g. to outline the text with, say, a 3px black outline to set it off 
from the background.) E.g.:

// Begin Snippet
   // m_text is a QTextDocument with HTML set using setHtml()  

   QPen pen;
   pen.setWidthF(3);
   pen.setColor(QColor(0,0,0,200));
   QBrush brush(QColor(255,255,255,200));
   painter->setPen(pen);
   painter->setBrush(brush);

    QAbstractTextDocumentLayout::PaintContext pCtx;
    m_text->documentLayout()->draw(painter, pCtx);
// End Snippet

Now, ideally, that would render with a 3px black border and white text. 
Of course, as we all know, HTML can spec its own text color inline - 
which would imply that the documentLayout() overrides the pen already 
set on the provided painter.

Is their any way to tell the documentLayout()->draw() method what pen to 
use or to force it to use the pen / brush on the painter? (And yes, I've 
googled, and I've tried drawing the QTextBlocks myself - but I want 
wordwrapping and I cant figure out how to wordwrap it myself from the 
QTextBlocks. Oh, and no, pCtx doesn't have a method for specifying the 
pen that I've seen. But I keep searching. Hence this email.)

Any ideas or help is welcome. Preferably with a code snippet, as I'm not 
very solid in my understanding of this area.

Thanks for your help in advance!
-josiah





More information about the Qt-interest-old mailing list