PDA

View Full Version : Need help with my delegate



Guilo
30th June 2010, 14:43
Hi !

I am using a custom delegate to display my qlistview. Indeed, I want each of the items to be written on two lines and using html syntax to highlight it. No need for editing the items so I have just reimplemented the painter method :


void AlertDelegate::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();
}

But the problem is that the first line of the second item is written on the second line of the first item. Where is the problem ?


Thanks

tbscope
30th June 2010, 16:06
You should also implement sizeHint() to let the view know how big each item is.
I guess in your case, you could just double the height (more or less).

Guilo
1st July 2010, 10:41
Is sizeHint a method of QItemDelegate or an attribute of QItemDelegate ? Or an attribute of my QTextDocument ? Any example somewhere to implement this ? cause I don't know how to double the actual value.

Thanks

tbscope
1st July 2010, 10:55
sizeHint() is a function of a widget which returns how big the widget is.

Example:

QSize AlertDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// Do some checks with the text document so you know the returned value is correct. If you use a pointer for example, check if it exists. etc...

return myDoubleLineTextDocument.sizeHint();
}

You will need to define your textdocument more accessible in your delegate.

Guilo
1st July 2010, 11:55
QTextDocument has no member sizeHint()cause it inherits QObject and not QWidget. And what about painter and context ?

tbscope
1st July 2010, 12:01
It has a size() function.

Or whatever else that you can use to get the size of the text.

Guilo
1st July 2010, 12:15
It works quite great. But each time I update my model (with clear() and appendRow()), some space must appear on the right side of the text beacause the horizontal scrollbar gets smaller each update.