PDA

View Full Version : Displaying richtext on ItemWidgets



arbi
25th August 2008, 08:14
Hi,

i am new at QT and just started to read Qt examples and tutorials.

i want to display richtext formatted by using HTML-tags ,like QLabel does, in the qt item widgets ( i.e QListWidget,QTableWidget etc. ). I don't know what should my starting point. Defining a custom delegate or inheriting Item classes. Or do i have to use item views instead?

what is the easiest way for that?

wysota
25th August 2008, 08:48
The easiest way is to search the forum for similar issues.

arbi
25th August 2008, 12:26
i have found some similar issue to my question at the forum thread :

http://www.qtcentre.org/forum/f-qt-programming-2/t-drawing-a-widget-in-qitemdelegates-paint-method-8660.html


there , QTextDocument is been mentioned as the solution to render rich text
on the paint() method of custom Delegate.

QTextDocument::drawContents() is introduced by Qt 4.2

İ am using Qt. Coomercial 4.1.3.

arent there any "Qt. 4.1.3 before" solution for rendering RichText on paint().?


BTW; i keep searching on this both at forum and swhr else. no need to remind that


thanks...

wysota
25th August 2008, 12:32
You can still you QTextDocument through its text layout, as far as I remember.

arbi
25th August 2008, 14:52
ok.

i finally found one of your early posts on this topic which gives a sample code



void CommentDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
painter->save();
QTextDocument doc;
doc.setHtml( index.data().toString() );
QAbstractTextDocumentLayout::PaintContext context;
doc.setPageSize( option.rect.size());
painter->translate(option.rect.x(), option.rect.y());
doc.documentLayout()->draw(painter, context);
painter->restore();
}


i used this method and it worked. But now there is another problem of obtaining
right size values to be returned from sizeHint() function.

i am reimplementing the sizeHint() method of the Delegate and for the time being, i am just returning a constant size of QSize(200,250). But of course this should be determined by the content of the item to be displayed. For large contents; some of the text are just lost behind the content of next item.

i tried QTextDocument::pageSize() and QTextDocument::documentLayout()->documentSize() methods , after setting the content of the QTextDocument object , but it didnt worked.

any suggestions appreciated.. thanks...