CheckBox and selection in QTableView
Hi
I have a QTableView with the selection set to:
m_table->setSelectionBehavior(QAbstractItemView::SelectRow s);
m_table->setSelectionMode(QAbstractItemView::ExtendedSelec tion);
the associated model contains elements with the Qt::CheckStateRole role and flags
set to:
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable;
When the user clicks on a checkbox it all nicely changes state and all. However is also changes the selection. ie if the user checks on a box of an item in a non selected row, that row becomes selected.
I tried overridding the EditorEvent in a QItemDelegate() but the selection change happens before it reaches that code.
Any idea how I can prevent the selection from being changed when the user click in the check box (but work as before if clicked outside) ?
Cheers
Re: CheckBox and selection in QTableView
QAbstractItemView updates the selection in mouse event handlers. You will have to override at least mousePressEvent() and prevent QAbstractItemView from handling the event if mouse was pressed over the checkbox. You will most propably have to add some checks in mouseReleaseEvent() too, otherwise the selection behaviour might get broken.
Here's the code for detecting if a mouse event occurs over item's checkbox:
Code:
if (idx.isValid())
{
opt.rect = visualRect(idx);
QRect rect
= style
()->subElementRect
(QStyle::SE_ViewItemCheckIndicator,
&opt
);
if (rect.contains(event->pos()))
{
// pressed/released over the checkbox...
}
}