[Qt-interest] How to render basic HTML in QTreeView cells?
Paul Colby
qt at colby.id.au
Tue Sep 29 04:47:04 CEST 2009
Thank you very much josiah! :)
You definitely pointed me in the right direction :)
It took a bit to figure out how to get QTextDocument::drawContents to
paint at the correct location, so for anyone else interested, here's
how I got it to work (overriding QStyledItemDelegate::paint):
QTextDocument doc;
doc.setTextWidth(option.rect.width());
doc.setHtml(index.data().toString());
painter->save();
painter->translate(option.rect.topLeft());
doc.drawContents(painter,QRect(QPoint(0,0),option.rect.size()));
painter->restore();
The trick here is the QPainter::translate call.
QTextDocument::drawContents draws from point 0,0 and only uses its
rect parameter for clipping. So the translate call shifts all of
drawContents' operations to the right location, and then drawContents
is told to clip to the same rect size, but starting at offset 0,0
instead. This works very nicely (of course,
QStyledItemDelegate::sizeHint should now be overridden too).
(PS - Resizing the QTreeView control is *much* faster painting this
way (using a custom delegate) than using the QTreeView::setIndexWidget
approach).
Thanks again! :)
paul.
On Tue, Sep 29, 2009 at 10:56 AM, Josiah Bryan
<jbryan at productiveconcepts.com> wrote:
> Paul Colby wrote:
>>
>> Hi,
>>
>> I want to use Qt's basic HTML support within some QTreeView cells, and
>> I'm trying to figure out the best / right way to do it.
>>
>> I've worked out that using QTreeView:setIndexWidget() with a widget
>> that supports HTML text (eg QLabel, QTextEdit, etc) works - albeit
>> with a number of issues (that are hopefully solvable).
>>
>> However, I'm also thinking that it might be better to simply override
>> QStyledItemDelegate::paint() to paint HTML text directly, but I can't
>> see how to paint HTML text there.
>>
>
> Paul,
>
> I'm not sure of QTextView specific paiting, but I am familiar with painting
> HTML - see QTextDocument::drawContents(QPainter*,const QRectF&).
>
> Something like:
>
> QTextDocument doc;
> doc.setHTML("<b>Hello</b> <i>World</i>!");
> doc.drawContents(painter);
>
> HTH.
>
> Regards,
> -josiah
>
>
--
http://colby.id.au
More information about the Qt-interest-old
mailing list