[Interest] Any idea why QAbstractItemView::selectedIndexes() is protected?

André Somers andre at familiesomers.nl
Wed Jul 4 09:41:40 CEST 2012


Op 3-7-2012 17:02, K. Frank schreef:
> Hello André!
>
> Thanks for the further explanation.
You're welcome.
>
> On Tue, Jul 3, 2012 at 10:10 AM, André Somers <andre at familiesomers.nl> wrote:
>> Op 3-7-2012 15:48, K. Frank schreef:
>>> Hi André!
>>>
>>> Thank you for the reply and the pointer to QItemSelectionModel.
>>>
>>> On Tue, Jul 3, 2012 at 2:06 AM, André Somers <andre at familiesomers.nl> wrote:
>>>> ...
>>>> I don't know exactly, but the public API for manipulating selections is
>>>> QItemSelectionModel.
>> ...
>> Keep in mind that views _have_ an item selection, but they are not quite
>> bound to them.
> Ah, I missed that.
>
> Just to check that I understand:  If I don't do anything special by hand
> (that is, I don't fiddle with selection machinery, or call anything like
> setSelectionModel), then if I call
>
>     QAbstractItemView::selectionModel ()
>
> I get a pointer to the view's "built-in" selection model, and it will tell me
> (among other things) which items in the view are currently selected?
>
> If this is the case I should be considering using selectionModel(), and
> the fact that selectedIndexes() is protected doesn't really matter.
Yes. I by default, each view constructs its own instance of 
QItemSelectionModel. It is perfectly save to use the pointer you get to 
that instance from QAbstractItemView::selectionModel().
>
>> The current design makes it possible, for instance, to
>> easily share the item selection between two or more views by simply
>> setting the same QItemSelectionModel on multiple views.
> Yes, thank you.  This is all making more sense now.
Sharing an item selection between two views (on the same model) is as 
easy as doing:
view2->setSelectionModel( view1->selectionModel() );

Note that using proxy models will hinder this though. Steveire wrote a 
selection model class to solve that issue.

André




More information about the Interest mailing list