Qt - QPushButtons in place of QTreeView Items
Is it possible to add QPushButtons for every item in a QTreeView? For instance, when you click on a TreeItem (that is a button), it's children开发者_StackOverflow中文版 get displayed as buttons as well? I just have a standard QTreeView.
_layout = new QVBoxLayout(this);
treeView = new QTreeView(this);
QStandardItemModel* standardModel = new QStandardItemModel();
QStandardItem* rootMenu = standardModel->invisibleRootItem();
//populate TreeView
treeView->setModel(standardModel);
treeView->setWordWrap(true);
treeView->setHeaderHidden(true);
//treeView->expandAll();
_layout->addWidget(treeView);
this->setLayout(_layout);
I have not personally done this (yet), but you could try using QAbstractItemView::setIndexWidget(). The widgets won't aren't connected in any way to the data model, so it is up to your code to update them if necessary. Also, you need to call it for each QModelIndex separately.
Here is the answer. You must create your own delegate and applay it for your QTreeView
.
To create delegate you must subclass QStyledItemDelegate and re-implement its QStyledItemDelegate::paint(...)
method in that way what you want, also, don't forget about re-implementing QStyledItemDelegate::sizeHint(...)
method if needed, of course.
Also, you may need to re-implement QStyledItemDelegate::createEditor(...)
method.
To apply created delegate to your view (QTreeView
) you must create delegate and call QTreeView
's method setItemDelegate
(or setItemDelegateForColumn
, or setItemDelegateForRow
).
Good luck!
精彩评论