Did you notice that item selection is changed due to mouse press events and the check state is not changed until mouse release events..?
Originally Posted by Arthur
Anyhow, as I mentioned in the earlier post, you have to catch certain mouse events in order to prevent the view from acting like it normally does.
Try these steps to solve the problem:
- Override both, mousePressEvent and mouseReleaseEvent. Basically you want to handle these events always when they occur over an item's checkbox, and otherwise pass them over to the base class to handle it's contiguous selection.
- Whenever a mouse press event (or release event, whichever you prefer) occurs over an item's checkbox, change the check state of all selected items.
The hardest part might be calculating whether a mouse press/release event's position is inside an item's checkbox. You can use QStyle for that.
This is just for reference:
// contiguous selection mode
// add some dummy checkable example items
for (int i = 0; i < 10; ++i)
// mouse pressed over a selected items checkbox,
// change the check states of all selected items
setSelectedCheckStates(item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked);
// otherwise let the base class handle mouse press
// do nothing if mouse released over a selected items checkbox
// otherwise let the base class handle mouse release
// returns the selected item whose checkbox lies under 'pos'
// returns 0 if not selected, no item at pos, or does not fit inside checkbox
// with the help of styles, check if checkbox rect contains 'pos'
opt.rect = visualItemRect(item);
// assure that the item is also selected
if (selectedItems().contains(item) && r.contains(pos))
// sets the check state of all selected items to 'checkState'
void setSelectedCheckStates(Qt::CheckState checkState)
int main(int argc, char *argv)
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
To copy to clipboard, switch view to plain text mode