树视图列中的 QPushButtons [英] QPushButtons in a column of a treeview
问题描述
我已经有一个树模型视图,其中包含带有标签、文本编辑和复选按钮的列.我要添加的是一个按钮.这是我卡住的地方:
I already have a tree model view that contains columns with a label, text edit, and a check button. What I am trying to add is a push button. Here is where I'm stuck:
- 在flags"函数中,我将使用哪个命名空间?
- 在数据"功能中,按钮的作用是什么?(例如在检查按钮的情况下,我使用了
Qt::CheckStateRole
) - 在数据"函数(返回一个
QVariant
)中,我应该返回什么?创建的按钮?
- In the "flags" function which namespace will I use?
- In the "data" function what would be the role of the push button? (for example in the check button case I used the
Qt::CheckStateRole
) - In the "data" function (which returns a
QVariant
) what should I return? The button created?
我查看了有关此主题的其他答案,以及使用 setIndexWidget
建议的最受欢迎的答案,但我不确定如何.最后一点,我试图不使用 UI 设计器以编程方式执行此操作.
I have looked at other answers regarding this topic and the most popular answer suggested using a setIndexWidget
however I am not sure how. Last note, I am attempting to do this programmatically not using the UI designer.
谢谢!
推荐答案
首先,您似乎对 Qt 的模型/视图框架的工作方式有些困惑.我建议你去查看 Qt 文档:http://doc.qt.io/qt-5/model-view-programming.html
Before anything else, you seem a bit confused about how Qt's model/view Framework is working. I suggest you go check Qt documentation about it: http://doc.qt.io/qt-5/model-view-programming.html
您的 3 个问题的答案:
The answers to your 3 questions:
您不需要在
Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
中使用任何命名空间.只需返回适合您的索引的特定Qt::ItemFlags
(即Qt::ItemFlag
的组合).这与您希望在视图中使用QPushButton
的事实无关.我认为您对Qt::ItemIsUserCheckable
标志感到困惑,这使视图显示一个复选框.然而,这并不是这个标志的真正作用,实际上它只是告诉视图为用户提供一种方法来更改索引的Qt::CheckStateRole
.视图的默认行为是通过显示QCheckBox
来实现的.
You do not need to use any namespaces in
Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
. Just return the particularQt::ItemFlags
(i.e combination ofQt::ItemFlag
) which suits your index. This has nothing to do with the fact that you want aQPushButton
in your view. I think your are confused by theQt::ItemIsUserCheckable
flag, which make the view display a check box. However that is not what this flag really does, actually it just tells the view to offer a mean for the user to change theQt::CheckStateRole
of the index. The default behaviour of the view is to do it by display aQCheckBox
.
没有与按钮相关的角色.您可以使用Qt::CheckStateRole
或Qt::EditRole
,这取决于您选择显示QPushButton
的方法.
There are no roles associated with push buttons. You can use Qt::CheckStateRole
or Qt::EditRole
, it depends on the method you choose to display the QPushButton
.
在QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const
中,当role
为Qt::CheckStateRole
或 Qt::EditRole
,你可以返回一个布尔值来控制 QPushButton
的状态.但是,您永远不应该返回 QWidget
(或派生的),模型处理数据,而不是它们的显示方式.
In QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const
, when role
is Qt::CheckStateRole
or Qt::EditRole
, you may return a boolean which will control the state of the QPushButton
. However, you should NEVER return a QWidget
(or derived), the model handles data, not how they are displayed.
解决办法:重新实现QAbstractItemDelegate
(或QStyledItemDelegate
).重写:
A solution:
Reimplement QAbstractItemDelegate
(or QStyledItemDelegate
). Rewrite:
createEditor()
:创建一个QPushButton
.setEditorData()
:使用您在QAstractItemModel::data()
中使用的索引和角色设置QPushButton
状态.立>setModelData()
:根据QPushButton
状态更新模型.
createEditor()
: create aQPushButton
.setEditorData()
: set theQPushButton
state using the index and the role you used inQAstractItemModel::data()
.setModelData()
: update the model according to theQPushButton
state.
在您的视图上设置您的委托 (QTreeView::setItemDelegateForColumn()
).此时,您只会在编辑时获得一个按钮.然后您可以调用 QAbstractItemView::openPersistentEditor()
使其始终可见.
Set your delegate on your view (QTreeView::setItemDelegateForColumn()
). At this point you will get a push button only when in edition. You can then call QAbstractItemView::openPersistentEditor()
to make it always visible.
这篇关于树视图列中的 QPushButtons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!