PDA

View Full Version : Add QComboBox in a QTreeView with QAbstractItemModel



mut
8th September 2014, 05:45
Hello;

I have a QTreeView using a model subclassing QAbstractItemModel.

I want one of the item (QModelIndex) to be QComboBox?

Thank you for any help.

Mut.

wysota
8th September 2014, 07:53
Implement a custom delegate that will return a combo box for these items.

anda_skoa
8th September 2014, 07:53
If you want a cell editable as a combobox, then you need an item delegate that creates a combobox as the editor for that cell.

See QStyledItemDelegate for an often suitable base class.

Cheers,
_

mut
8th September 2014, 16:51
Thank you for the reply.

This thread http://www.qtcentre.org/threads/43148-QComboBox-in-QTreeView shows how to implement a delegate.
However, I'm wondering if "TreeView->setItemDelegateForColumn(8, delegate);" will not put a combobox in all the cells of the column?

What is the API the put the combobox into a specicifc QModelIndex?

I wonder if there is a clean example out there with not only how to create a delegate but also how to use it with QtreeView-QAstractItemModel?

Mut.

mut
8th September 2014, 18:59
QComboBox *cb = new QComboBox;
cb->addItem("a");
treeView->setIndexWidget(currentIndex,cb);

I have used above code and its seems to work. what is the difference between the delegate approach and the setIndexWidget() approach?

wysota
9th September 2014, 08:36
setIndexWidget does not bind the widget with the model in any way.

mut
10th September 2014, 18:24
Thanks for the reply.
I'm using signal/slot to connect with the model. It is working fine.
How to delete a combobox that was added with setIndexWidget()?
Use case is: 1) the row holding the combobox is deleted 2) the combobox needs to be relocated to another location due to tree layout change

anda_skoa
11th September 2014, 15:02
However, I'm wondering if "TreeView->setItemDelegateForColumn(8, delegate);" will not put a combobox in all the cells of the column?

Yes, but the delegate can easily decide not to do its special behavior for a given model index, i.e. just call the base implementation for cells it doesn't feel responsible for.

Cheers,
_