PDA

View Full Version : QAbstractItemDelegate and sizeHint issue



Lykurg
11th July 2008, 15:31
Hi,

in a more complicated case I need to determinate the hight of a text block in sizeHint(). Therefore I test it on the following code.


class MyItemDelegate : public QAbstractItemDelegate
{
public:
MyItemDelegate(QObject *parent=0) : QAbstractItemDelegate(parent)
{}
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
const QString text = index.data(Qt::DisplayRole).toString();
QRect r = option.rect.adjusted(2, 2, -2, -2);
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, text, &r);
}
QSize sizeHint ( const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
const QString text = index.data(Qt::DisplayRole).toString();
QRect r = option.rect.adjusted(2, 2, -2, -2);
QSize s = option.fontMetrics.boundingRect(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, text).size();
return s;
}
};

The problem for me is, that "option.rect" doesn't represent the same QRect in the two functions. How is it possible to determinate the hight of the drawn text in sizeHint().

Thanks,
Lykurg