PDA

View Full Version : CheckBox and selection in QTableView



Mike Krus
21st September 2006, 15:37
Hi

I have a QTableView with the selection set to:
m_table->setSelectionBehavior(QAbstractItemView::SelectRows );
m_table->setSelectionMode(QAbstractItemView::ExtendedSelect ion);

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

jpn
21st September 2006, 21:31
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:


QModelIndex idx = indexAt(event->pos());
if (idx.isValid())
{
QStyleOptionButton opt;
opt.QStyleOption::operator=(viewOptions());
opt.rect = visualRect(idx);
QRect rect = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt);
if (rect.contains(event->pos()))
{
// pressed/released over the checkbox...
}
}