PDA

View Full Version : QAbstractItemView selection behaviour issue



Caius Aérobus
30th April 2007, 22:52
Hello,
I get a little bit confused with the way selection behaviour in MVC views should be handled. More more precisely there are 2 places where selection behaviour can be described:

1) I put these lines in the constructor of my view:
this->setSelectionBehavior(QAbstractItemView::SelectRows );
this->setSelectionMode(QAbstractItemView::ExtendedSelect ion);
2) In response to some event I put these lines in a mouseReleaseEvent() function
QItemSelectionModel::SelectionFlags flags(QItemSelectionModel::Rows | QItemSelectionModel::SelectCurrent);
if (!this->ctrl_key)
flags |= QItemSelectionModel::Clear;
this->setSelection(r, flags);
and of course in the setSelection() method, I put:
this->selectionModel()->select(sel, flags);
where sel is a QItemSelection.

I would expect either 1) or 2) be enough for the extended selection behaviour to work, that is clicking on an item while handling the control key (in this case this->ctrl_key == 1) should select a new item while keeping the already selected ones selected. But even with both codes it does not work!
So my questions are:
- which mistake(s) in my code?
- should I use 1) and/or 2) for the extended selection behaviour to work?
Thanks in advance for your help.

Caius

PS: I use Qt4.1

marcel
30th April 2007, 23:11
How do you determine that the control key is pressed?

Caius Aérobus
30th April 2007, 23:43
How do you determine that the control key is pressed?

in a keyPressedEvent() slot, which makes this->ctrl_key = 1;

marcel
30th April 2007, 23:53
I am afraid this is not correct. You must determine the modifier keys in the mouse event, with QMouseEvent::modifiers().

regards

Caius Aérobus
1st May 2007, 16:33
I am afraid this is not correct. You must determine the modifier keys in the mouse event, with QMouseEvent::modifiers().

regards

Thanks for the tips, it is more elegant and easier to do like this BUT the problem was not here since my way of detecting ctrl key press prior to clicking on an item works too (I printed the value of ctrl_key to check it of coursee) so the clear flag is off and I would expect that the already selected items remain selected, which is not the result I obtain.
Any help?