[Qt-interest] Rich Text: Custom Pen and Word Wrap

Josiah Bryan jbryan at productiveconcepts.com
Sun Sep 13 04:26:49 CEST 2009


This is a bit of a rant, combined with a cry for help. After emailing 
yesterday about painting a QTextDocument with a custom pen, I've kept 
googling and searching thru the docs and examples. It feels impossible 
to do what I want to do. I know I can draw a QTextDocument with a Custom 
Pen (I've got code to do that now,) and I know how to draw a 
QTextDocument with word wrapping to a app-defined width. What I *dont* 
know how to do is to do both at the same time!!

The code to draw the rich text with a custom pen does something like this:

for (QTextBlock tb = m_text->begin(); tb.isValid(); tb = tb.next()) {
     ....
     for (QTextBlock::iterator tbIt = tb.begin(); !(tbIt.atEnd()); ++tbIt) {
        QTextCharFormat format = frag.charFormat();
            QFont font = format.font();
            painter->setFont(font);
            painter->setPen(myPen);
            painter->setBrush(myBrush);
            QString text = frag.text();
            foreach (const QChar & textChar, text)  { /* draw the 
character */ }
}}

Anyway, you get the idea. (Full code available if desired.)

And, I can draw with wordwrapping.

    QTextOption t;
    t.setWrapMode(QTextOption::WordWrap);
    for (QTextBlock tb = m_text->begin(); tb.isValid(); tb = tb.next())
           tb.layout()->setTextOption(t);
     m_text->setTextWidth(contentsRect().width());
     QAbstractTextDocumentLayout::PaintContext pCtx;
     m_text->documentLayout()->draw(painter, pCtx);

All groovy, right? Well, I can't figure out how to do my own wrapping in 
my rendering code (because, how do I break apart and wrap text 
fragments? Do I wrap each character? Do I put in my own word break code? 
Crikey), and I cant figure out how to convince this line:      
m_text->documentLayout()->draw(painter, pCtx);  to use my own pen!

Any ideas?

Thanks!
-josiah
      
 




More information about the Qt-interest-old mailing list