QTreeView custom row height of individual rows

强颜欢笑 提交于 2019-12-13 18:18:42

问题


Is it possible to redefine the row height of certain individual rows within a QTreeView?

I have a custom QTreeView, custom QAbstractItemModel and a custom QStyledItemDelegate, but it seems that all the sizeHint methods are either called only once (initially) or are not virtual in the base classes.

Qt Version 4.7.4, no upgrade to 5 possible.

Any help appreciated.


回答1:


Reimplement the delegate’s sizeHint(). Found an example in some production code of mine. It is shown simplified below. In the example, the tree may contain images. Therefore the cell sizes need to be adjusted to host the images.

class ItemDelegate : public QItemDelegate
{
  public:
      QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
      {
           const TreeItem* ti(static_cast<TreeItem*>(index.internalPointer()));
           if(ti->pixmap())
              return ti->pixmap()->size();
           QItemDelegate::sizeHint(option,index);
      }
};

Usage:

 QTreeView view;
 ItemDelegate *delegate = new ItemDelegate;
 view.setItemDelegate(delegate);


来源:https://stackoverflow.com/questions/27859958/qtreeview-custom-row-height-of-individual-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!