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
The answers to your 3 questions:
You do not need to use any namespaces in Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
. Just return the particular Qt::ItemFlags
(i.e combination of Qt::ItemFlag
) which suits your index. This has nothing to do with the fact that you want a QPushButton
in your view. I think your are confused by the Qt::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 the Qt::CheckStateRole
of the index. The default behaviour of the view is to do it by display a QCheckBox
.
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
.
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.
A solution:
Reimplement QAbstractItemDelegate
(or QStyledItemDelegate
). Rewrite:
createEditor()
: create a QPushButton
.
setEditorData()
: set the QPushButton
state using the index and the role you used in QAstractItemModel::data()
.
setModelData()
: update the model according to the QPushButton
state.
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.