QTreeView 自定义各行的行高 [英] QTreeView custom row height of individual rows
问题描述
是否可以重新定义 QTreeView
中某些单独行的行高?
Is it possible to redefine the row height of certain individual rows within a QTreeView
?
我有一个自定义的 QTreeView
、自定义的 QAbstractItemModel
和一个自定义的 QStyledItemDelegate
,但似乎所有的 sizeHint
> 方法要么只调用一次(最初),要么在基类中不是虚拟的.
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 4.7.4 版,无法升级到 5.
Qt Version 4.7.4, no upgrade to 5 possible.
感谢任何帮助.
推荐答案
重新实现委托的 sizeHint().在我的一些生产代码中找到了一个例子.下面将其简化显示.在示例中,树可能包含图像.因此需要调整单元格大小以承载图像.
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);
}
};
用法:
QTreeView view;
ItemDelegate *delegate = new ItemDelegate;
view.setItemDelegate(delegate);
这篇关于QTreeView 自定义各行的行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!