Qt: start editing of cell after one click

Ashot picture Ashot · Sep 16, 2013 · Viewed 14.6k times · Source

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.

Answer

shijq73 picture shijq73 · Jul 3, 2015

You can just set edit trigger use this function setEditTriggers

C++

yourView->setEditTriggers(QAbstractItemView::AllEditTriggers)

Python:

yourView.setEditTriggers(QAbstractItemView.AllEditTriggers)

enum QAbstractItemView::EditTrigger flags QAbstractItemView::EditTriggers

This enum describes actions which will initiate item editing.

Constant    Value   Description
QAbstractItemView::NoEditTriggers   0   No editing possible.
QAbstractItemView::CurrentChanged   1   Editing start whenever current item changes.
QAbstractItemView::DoubleClicked    2   Editing starts when an item is double clicked.
QAbstractItemView::SelectedClicked  4   Editing starts when clicking on an already selected item.
QAbstractItemView::EditKeyPressed   8   Editing starts when the platform edit key has been pressed over an item.
QAbstractItemView::AnyKeyPressed    16  Editing starts when any key is pressed over an item.
QAbstractItemView::AllEditTriggers  31  Editing starts for all above actions.

The EditTriggers type is a typedef for QFlags. It stores an OR combination of EditTrigger values.