user375566
user375566

Reputation:

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 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);

Upvotes: 0

Views: 2668

Answers (2)

NG_
NG_

Reputation: 7181

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!

Upvotes: 0

Paul Du Bois
Paul Du Bois

Reputation: 2181

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.

Upvotes: 1

Related Questions