Qtreeview,在选择线条时调整其大小
我正在使用 Qt 4.7.0,一个具有多列的 Qtreeview。
我想做的是“简单”:我想要一条线在被选择时增加其高度。
代表们足以做到这一点吗?
我已经使用 QTableView 经历了一些事情:
m_pMyTableView->verticalHeader()->setResizeMode(QHeaderView::Interactive);
...
QSize AbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
它正在使用这个表视图,但我不知道如何在 QTreeview 上执行此操作,因为首先,它没有任何垂直标题......
有人可以照亮我的道路吗?
I'm using Qt 4.7.0, a Qtreeview with multiple columns.
What I want to do is "simple" : I want a line to increase its height, when it's selected.
Will delegates be enough to do this ?
I've been through some stuff with a QTableView :
m_pMyTableView->verticalHeader()->setResizeMode(QHeaderView::Interactive);
...
QSize AbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
It's working with this tableview, but I can't see how I'll do this on a QTreeview, since, for a start, it doesn't have any vertical headers...
Can someone enlight my path please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了在
QTreeView
中设置uniformRowHeights
关闭之外,我还会尝试这样做。有几种方法可以做到这一点,我喜欢使用 Qt 的信号/槽,因此我们将通过
QTreeView
上的自定义QAbstractItemModel
来更改高度。此自定义模型将连接到来自QTreeView
的QItemSelectionModel
的信号selectionChanged
。示例代码/片段适用于单选模式,但您可以轻松更改它以处理多个选定的行。第 1 步 - 使用选择槽创建自定义模型
创建从
QAbstractItemModel
派生的自定义模型类,并确保创建一个槽,例如:在模型类中添加以下代码片段/方法。
第 2 步 - 将选择更改连接到您的模型
在
QTreeView
的初始化中创建自定义模型并执行以下操作:我确信有几种方法可以做到这一点,即将 QItemSelectionModel 直接交给您的 QAbstractItemModel ,但我还是更喜欢使用信号/槽并将选择保存在模型中。
希望这有帮助。
Along with setting
uniformRowHeights
off in in yourQTreeView
here is what I would try.There are few ways to do this, I like to use Qt's signals/slots, as such we're going to change the height through a custom
QAbstractItemModel
on theQTreeView
. This custom model will be connected to the signalselectionChanged
from theQItemSelectionModel
of yourQTreeView
. The example code/snippets work with single selection mode but you can easily alter it to handle multiple selected rows.Step 1 - Create Custom Model with Selection slot
Create the custom model class that derives from
QAbstractItemModel
and make sure you create a slot such as:Inside your model class add the following snippets/methods.
Step 2 - Connect Selection Changes to Your Model
Inside the initialization of your
QTreeView
create your custom model and do the following:I'm sure there are a few ways of doing this, i.e. handing the
QItemSelectionModel
directly to yourQAbstractItemModel
but again I prefer to use signals/slots and to save the selection in the model.Hope this helps.