By default the cell in QTableView
starts being edited after double click. How to change this behavior. I need it to start editing after one click.
I have set combo-box delegate to the cell. When clicking the cell it only selects it. When double clicking on the cell the QComboBox
editor is activated but not expanded. I want it to expand after just one click as if I added QComboBox
by setCellWidget
function of QTableWidget
. I need the same effect by using model-view-delegate.
Edit after one click You can reimplement mousePressEvent in view you are using
Expanded QCombobox when edit You should imlement setEditorData in your subclass of QItemDelegate and at the end call showPopup.
But it has some unexpected behaviour. QComboBox disappears when mouse leave its area. But for me it is advantage. I can select different item with single click and release.
Together it works fast way. Click and hold to choose item and commit data on release ( Just one click and release )
If you want click to show expanded qcombobox and next click to choose/hide, I do not know solution for now.
You can just set edit trigger use this function setEditTriggers
C++
Python:
enum QAbstractItemView::EditTrigger flags QAbstractItemView::EditTriggers
This enum describes actions which will initiate item editing.
The EditTriggers type is a typedef for QFlags. It stores an OR combination of EditTrigger values.
This solution works perfeclty for me. Single click on a cell, and the combo pops up.
If you override
QStyledItemDelegate::createEditor()
then you can expand the combo box after it is created.Based on the idea provided by Jason, I came up with this solution.
To launch the editor on single click, I connected
QAbstractItemView::clicked(const QModelIndex &index)
signal of my view, toQAbstractItemView::edit(const QModelIndex &index)
slot of that same view.If you use Qt4, you need to create a slot in your delegate. Pass your combobox as an argument to this slot. In this slot you call
QComboBox::showPopup
. So it will look like this:But first we need to register the
QComboBox*
type. You can call this in the constructor of your delegate:The reason we need this slot, is because we can't show the pop up straight away in
MyDelegate::createEditor
, because the position and the rect of the list view are unknown. So what we do is inMyDelegate::createEditor
, we call this slot with a queued connection:This will show the list view of the combobox correctly when the editor is activated.
Now if you are using Qt5, the slot is not needed. All you do is call
QComboBox::showPopup
with a queued connection fromMyDelegate::createEditor
. The easiest way to do this is with aQTimer
:For some extra information, this is how you can paint the combobox so it is shown all the time, not only when the editor is shown: