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.

Qt Code:
  1. class MyItemDelegate : public QAbstractItemDelegate
  2. {
  3. public:
  4. MyItemDelegate(QObject *parent=0) : QAbstractItemDelegate(parent)
  5. {}
  6. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
  7. {
  8. const QString text = index.data(Qt::DisplayRole).toString();
  9. QRect r = option.rect.adjusted(2, 2, -2, -2);
  10. painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, text, &r);
  11. }
  12. QSize sizeHint ( const QStyleOptionViewItem &option, const QModelIndex &index ) const
  13. {
  14. const QString text = index.data(Qt::DisplayRole).toString();
  15. QRect r = option.rect.adjusted(2, 2, -2, -2);
  16. QSize s = option.fontMetrics.boundingRect(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, text).size();
  17. return s;
  18. }
  19. };
To copy to clipboard, switch view to plain text mode 

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