0

QT table cell editing behaviour

I have table in QT using C++. I need to have : 1. Single cell should allow editing either if clicked on selected cell or double clicked on cell 2. Multi-editing is allowed when either double clicked on cell or clicked on any one of the selected cell How to achieve this? I have used SetSelectionMode(MultiSelection) and SetSelectionBehavior(SelectItems) along with SetEditTriggers(SelectedClicked | DoubleClick). This works well for single selection i.e. I am able to modify single cell value either by double click or by doing selection on selected cell. However, multi selection happens without me pressing control key. This is not desired and I need to have selection of multiple cells only using control key. Am I doing anything wrong or something additional is missing?

18th Apr 2025, 1:51 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 odpowiedzi
+ 2
Hi Ketan, You're on the right track using: setSelectionMode(QAbstractItemView::MultiSelection); setSelectionBehavior(QAbstractItemView::SelectItems); setEditTriggers(QAbstractItemView::SelectedClicked | QAbstractItemView::DoubleClicked); But your issue is that multi-selection is happening without holding the Control key, and that's not expected behavior. The Problem By default, QTableView allows multi-selection with mouse drag or just clicking multiple items, depending on how selection mode is set. The Solution To force multi-selection only when the Control key is pressed, you need to override mouse interaction behavior. Qt does not provide this exact behavior out of the box, so you can subclass the QTableView and override the mousePressEvent: void MyTableView::mousePressEvent(QMouseEvent *event) { if (!(event->modifiers() & Qt::ControlModifier)) { clearSelection(); // Prevents multi-select without Ctrl } QTableView::mousePressEvent(event); }
20th Apr 2025, 5:28 AM
Jenny Alava Bolaños
Jenny Alava Bolaños - avatar
0
Perfect ..! I got idea about what you are saying and can implement the code you suggested. However, I have an additional query. In my existing code base, I could not find a place where something related to Qt::ControlModifier has been used. Without this, It works fine for me to select multiple cells only with control key. This behavior works well , but gets disturbed when setEditTriggers(QAbstractItemView::SelectedClicked) is added to code. Am I missing something to check or is there another way also to achieve multi selection only with control key?
21st Apr 2025, 9:50 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Hi Jenny Alava Bolaños Please disregard my previous response as control modifiers was renamed a bit different in code base and was there. Your suggestion was helpful and solves my problem. Thanks again
21st Apr 2025, 10:57 AM
Ketan Lalcheta
Ketan Lalcheta - avatar