PDA

View Full Version : selectionModel() strange behavior



Str Anger
20th June 2016, 09:27
Hi everyone,

I got somehting really weird and I wonder if you guys could help me :

I have a QListView that displays my items, and I want to get the indexes from any Extended Selection.
So I get the signal like this :

connect(centralView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelect ion)), this, SLOT(selectInTrace(QItemSelection,QItemSelection)) );

But, the problem is : no matter how many item I select in my centralView, only the last one is emitted by the SIGNAL selectionChanged. At first, I thought the selectionModel wasn't selecting all the wanted items, but, in my SLOT, I tried to use centralView->selectionModel()->selection() (instead of the first QItemSelection related to the selected items) and it effectively returns all the desired items...

Of course, I have set the central view selection mode as :

centralView->setSelectionMode(QAbstractItemView::ExtendedSelect ion);

Which confirms that the problem does not come from the selectionModel itself, but rather from the SIGNAL (at least, that is the only logical cause I have figured out).

The problem is not a real problem itself (my app have a decent behavior by using the centralView->selectionModel()->selection() ) but if someone could tell me how to fix this little bug, I will be quite thankful, for I feel like my code is a bit trashy.

anda_skoa
20th June 2016, 10:44
Hmm.

Maybe the signal doesn't transport the new and the old state but the two changes, i.e. the things that are now additionally part of the selection (first argument) and the things that are no longer part of the selection (second argument).

You could try using the select() method to select more than one index in one go and check if all of them are in the first argument.

Cheers,
_

Str Anger
20th June 2016, 11:06
Hi anda, and thank you for your answer.

I'm not really sure I completely understood what you told me to do, but, in fact, I use the select() method.
Let me explain :

I have 2 QListViews, each one linked to 2 different proxy models, both of them having the same sourceModel.
When I select items on the first view, I want my second view to select the same item and scroll to it.
On the other hand, I have a button that, when activated, display only the selected items in my second view. The strange part is, when i check -> uncheck this button, the selection of the first view is now fully selected in the second (while it doesn't if I do nothing but select in the first view).

I hope this will help you a bit more :)

ChrisW67
20th June 2016, 21:50
The usual approach to ensuring two views show the same selection is to use the same selection model in both views. This, however, requires that the views have the same source; not a situation you have because of the proxy models.

The signal carries only the differences between the previous selection and the new selection: a set of newly selected items, and a set of newly unselected items. Your slot needs to map the deleted item indexes to indexes in the second model and unselect them from the existing selection on the second view. It should then repeat the activity for the newly selected items. To map the indexes will require the use of the proxy model mapToSource and mapFromSource functions (I think that is the names) because the selections will be proxy indexes and not source indexes.