Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 7

Qt-interest Archive, August 2006
How to determine if there is a selection in an QListView (Qt 4.1)


Message 1 in thread

I use the following code to determine, if there is a selection in my 
QListView:

    bool hasSelection = 
myList->selectionModel()->selectedIndexes().count() > 0;

But it somehow feels way to clumsy. Is there another, shorter, more 
elegant way to determine, whether the QListView has a selection?

Best regards,
Robert Lichtenberger

--
 [ signature omitted ] 

Message 2 in thread

On Thursday 31 August 2006 08:25, Robert Lichtenberger wrote:
> I use the following code to determine, if there is a selection in my
> QListView:
>
>     bool hasSelection =
> myList->selectionModel()->selectedIndexes().count() > 0;
>
> But it somehow feels way to clumsy. Is there another, shorter, more
> elegant way to determine, whether the QListView has a selection?

QItemSelectionModel also has a hasSelection() method :)

bool hasSelection = myList->selectionModel()->hasSelection();

That looks fine to me. Ah, wait, I see that hasSelection() is new in Qt 
4.2 :-}

Simon

Attachment:

Attachment: pgpiTC7l5Ctai.pgp
Description: PGP signature


Message 3 in thread

well, the selection model is actually the proper (elegant for design
freaks) way to deal with a selection. If you are looking for a
convenience method, that's another story (can't remmeber one in the
docs). I would stick with the selectedIndexes route because the idea
of "selection" gets hairy when you are using the "item" classes from
the convenient classes. (see QTableWidget selection thread from a
couple of days ago).

sorry

On 8/30/06, Simon Hausmann <simon.hausmann@xxxxxxxxxxxxx> wrote:
> On Thursday 31 August 2006 08:25, Robert Lichtenberger wrote:
> > I use the following code to determine, if there is a selection in my
> > QListView:
> >
> >     bool hasSelection =
> > myList->selectionModel()->selectedIndexes().count() > 0;
> >
> > But it somehow feels way to clumsy. Is there another, shorter, more
> > elegant way to determine, whether the QListView has a selection?
>
> QItemSelectionModel also has a hasSelection() method :)
>
> bool hasSelection = myList->selectionModel()->hasSelection();
>
> That looks fine to me. Ah, wait, I see that hasSelection() is new in Qt
> 4.2 :-}
>
> Simon
>
>
>


-- 
 [ signature omitted ] 

Message 4 in thread

Simon Hausmann schrieb:
> On Thursday 31 August 2006 08:25, Robert Lichtenberger wrote:
>   
>> I use the following code to determine, if there is a selection in my
>> QListView:
>>
>>     bool hasSelection =
>> myList->selectionModel()->selectedIndexes().count() > 0;
>>
>> But it somehow feels way to clumsy. Is there another, shorter, more
>> elegant way to determine, whether the QListView has a selection?
>>     
>
> QItemSelectionModel also has a hasSelection() method :)
>
> bool hasSelection = myList->selectionModel()->hasSelection();
>
> That looks fine to me. Ah, wait, I see that hasSelection() is new in Qt 
> 4.2 :-}
>   
So I'll adapt the code, once 4.2 is out.

Thanks,
Robert

--
 [ signature omitted ]