Hi,

I made a delegate for multiline entries for QListView. The problem I am facing right now is that the sizehint method is not accurate. If you run the sample code and slowly change the width of the listview you will recognice that on specific width a "item line" is too high. (sizeHint is calculating one text line too much. QPainter on the other hand can render the text in X - 1 line.)

Qt Code:
  1. #include <QtGui>
  2. #include <QtWidgets>
  3.  
  4. class Delegate : public QItemDelegate
  5. {
  6. Q_OBJECT
  7. public:
  8. explicit Delegate(QObject *parent = 0)
  9. : QItemDelegate(parent)
  10. , m_padding(6)
  11. {}
  12.  
  13. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
  14. {
  15. const QFontMetrics fm = option.fontMetrics;
  16. const QString &text = index.data().toString();
  17. const QRect r = QRect(0, 0, option.rect.width() - 2 * m_padding, 0);
  18.  
  19. QSize s = option.fontMetrics.boundingRect(r, Qt::AlignLeft | Qt::TextWordWrap, text).size();
  20. s.setHeight(s.height() + 2 * m_padding);
  21. s.setWidth(s.width() + 2 * m_padding);
  22. return s;
  23. }
  24.  
  25. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  26. {
  27. bool selected = option.state & QStyle::State_Selected;
  28.  
  29. if (selected)
  30. painter->fillRect(option.rect, option.palette.highlight());
  31. else if (option.features & QStyleOptionViewItem::Alternate)
  32. painter->fillRect(option.rect, option.palette.alternateBase());
  33.  
  34. painter->setPen(selected
  35. ? option.palette.highlightedText().color()
  36. : option.palette.text().color());
  37.  
  38. const QRect r = option.rect.adjusted(m_padding , m_padding , -m_padding , -m_padding);
  39. painter->drawText(r, Qt::AlignLeft | Qt::TextWordWrap, index.data().toString());
  40. }
  41.  
  42. private:
  43. int m_padding;
  44. };
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48. QApplication a(argc, argv);
  49.  
  50. l << "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vel ligula tellus.";
  51. l << "Quisque lorem leo, porta in bibendum vitae, pellentesque mollis dui. Suspendisse viverra sollicitudin volutpat. Pellentesque arcu est, dignissim vel tempus at, facilisis et diam.";
  52. l << "Suspendisse est urna, feugiat vel dignissim ut, aliquet nec eros.";
  53.  
  54. QStringListModel model(l);
  55. Delegate delegate;
  56.  
  57. QListView view;
  58. view.setModel(&model);
  59. view.setItemDelegate(&delegate);
  60. view.setWordWrap(true);
  61. view.setAlternatingRowColors(true);
  62. view.show();
  63.  
  64. return a.exec();
  65. }
  66.  
  67. #include "main.moc"
To copy to clipboard, switch view to plain text mode 

It is because QFontMetrics::boundingRect() is returning a larger bounding box, because it uses also the maximum left and right font bearings. But how can I calculate them in sizeHint? I have tried different calculation but all failed.


Thanks,
Lykurg