How can I change the row height of a QTreeView?

The delegate’s sizeHint() [qt.nokia.com] is used to determine the row height of the QTreeView, so you need to reimplement the delegate’s sizeHint() [qt.nokia.com] to return the size you want.

The following example illustrates how this can be done:

  1. #include <QtGui>
  2.  
  3. class ItemDelegate : public QItemDelegate
  4. {
  5. public:
  6.   ItemDelegate()
  7.   {}
  8.   QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
  9.   {
  10.     return QSize(50,50);
  11.   }
  12. };
  13. int main(int argc, char **argv)
  14. {
  15.   QApplication app(argc, argv);
  16.   QTreeView view;
  17.   QStandardItemModel *model = new QStandardItemModel;
  18.   model->insertRows(0, 4);
  19.   model->insertColumns(0, 4);
  20.   for (int row = 0; row < 4; row++) {
  21.     for (int col = 0; col < 4; col++) {
  22.       QModelIndex index = model->index(row, col);
  23.       model->setData(index, QVariant((row+1) *(col+1)).toString());
  24.     }}
  25.   ItemDelegate *delegate = new ItemDelegate();
  26.   view.setModel(model);  
  27.   view.setItemDelegate(delegate);
  28.   view.show();
  29.   return app.exec();
  30. }
  31.  

No comments

Write a comment

Sorry, you must be logged in to post a comment.