Qt-interest Archive, May 2008
Save QListWidget selection
Message 1 in thread
I am using a QListWidget in multiple selection mode. At one point in
my code I need to update the list , which involves clearing the list
and re-populating it. When this happens, I would like the current
selection in the list to remain unchanged. Obviously this can't happen
directly (since the list is being cleared), but might there be some
way to "save" the selection before clearing, and then "restore" it
after?
I did figure out that I can use a combination of
QListWidget::selectedItems and QListWidgetItem::text() to get a
QStringList of selected items, and of course, a loop combined with the
QStringList and the QListWidget::findItems function can give me a list
of the items in the "new" list corresponding to the selected items in
the "old" list, but my dilemma here is how to select them- unless I am
missing something, the QListWidget::setCurrentRow(int) function only
selects a single item, and calling it multiple times just changes
which single item is selected. So is there any good way to save and
restore a multiple-item selection? or do I need to figure out some way
to do an in-place update for this to work? Thanks.
-----------------------------------------------
Israel Brewster
Computer Support Technician
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------
--
[ signature omitted ]
Message 2 in thread
On Thursday 15 May 2008 00:29:32 Israel Brewster wrote:
> I am using a QListWidget in multiple selection mode. At one point in
> my code I need to update the list , which involves clearing the list
> and re-populating it. When this happens, I would like the current
> selection in the list to remain unchanged. Obviously this can't happen
> directly (since the list is being cleared), but might there be some
> way to "save" the selection before clearing, and then "restore" it
> after?
>
> I did figure out that I can use a combination of
> QListWidget::selectedItems and QListWidgetItem::text() to get a
> QStringList of selected items, and of course, a loop combined with the
> QStringList and the QListWidget::findItems function can give me a list
> of the items in the "new" list corresponding to the selected items in
> the "old" list, but my dilemma here is how to select them- unless I am
> missing something, the QListWidget::setCurrentRow(int) function only
> selects a single item, and calling it multiple times just changes
> which single item is selected. So is there any good way to save and
> restore a multiple-item selection? or do I need to figure out some way
> to do an in-place update for this to work? Thanks.
>
Without knowing precisely why you are deleting the current items, only to put
some of them back, it seems better to look at an in place-update. This would
be a single iteration over the existing items deciding whether or not to
keep/change them and thus preserving the selected items (less those deleted).
Depending on the number of items, etc., it would also prevent 'flicker' as
the list is deleted and re-built.
HTH
Nick
--
[ signature omitted ]
Message 3 in thread
Hi,
"Israel Brewster" <israel@xxxxxxxxxxxxxxxxxx> schreef in bericht
news:3F20BEC9-F70D-4B89-AE1E-02105D047CB8@xxxxxxxxxxxxxxxxxxxxx
> I am using a QListWidget in multiple selection mode. At one point in my
> code I need to update the list , which involves clearing the list and
> re-populating it. When this happens, I would like the current selection
> in the list to remain unchanged. Obviously this can't happen directly
> (since the list is being cleared), but might there be some way to "save"
> the selection before clearing, and then "restore" it after?
>
> I did figure out that I can use a combination of
> QListWidget::selectedItems and QListWidgetItem::text() to get a
> QStringList of selected items, and of course, a loop combined with the
> QStringList and the QListWidget::findItems function can give me a list of
> the items in the "new" list corresponding to the selected items in the
> "old" list, but my dilemma here is how to select them- unless I am
> missing something, the QListWidget::setCurrentRow(int) function only
> selects a single item, and calling it multiple times just changes which
> single item is selected. So is there any good way to save and restore a
> multiple-item selection? or do I need to figure out some way to do an
> in-place update for this to work? Thanks.
While I agree with Nicholas that you should look into updating your list
without clearing it, I also wanted to point you to the
QListWidgetItem::setSelected method. You can use that to programatically
changing which items are selected. For the rest, you can use the stringlist
method you used before. Depending on the size of your list, you may also
want to keep a QHash<QString, QListWidgetItem> to keep track of the items in
your list. Of course, this takes memory and time, so it is useless with
small lists.
André
André
--
[ signature omitted ]
Message 4 in thread
On May 14, 2008, at 9:34 PM, André Somers wrote:
> Hi,
>
> "Israel Brewster" <israel@xxxxxxxxxxxxxxxxxx> schreef in bericht news:3F20BEC9-F70D-4B89-AE1E-02105D047CB8@xxxxxxxxxxxxxxxxxx
> ...
>> I am using a QListWidget in multiple selection mode. At one point
>> in my code I need to update the list , which involves clearing the
>> list and re-populating it. When this happens, I would like the
>> current selection in the list to remain unchanged. Obviously this
>> can't happen directly (since the list is being cleared), but might
>> there be some way to "save" the selection before clearing, and
>> then "restore" it after?
>>
>> I did figure out that I can use a combination of
>> QListWidget::selectedItems and QListWidgetItem::text() to get a
>> QStringList of selected items, and of course, a loop combined with
>> the QStringList and the QListWidget::findItems function can give me
>> a list of the items in the "new" list corresponding to the
>> selected items in the "old" list, but my dilemma here is how to
>> select them- unless I am missing something, the
>> QListWidget::setCurrentRow(int) function only selects a single
>> item, and calling it multiple times just changes which single item
>> is selected. So is there any good way to save and restore a
>> multiple-item selection? or do I need to figure out some way to do
>> an in-place update for this to work? Thanks.
> While I agree with Nicholas that you should look into updating your
> list without clearing it, I also wanted to point you to the
> QListWidgetItem::setSelected method. You can use that to
> programatically changing which items are selected. For the rest, you
> can use the stringlist method you used before. Depending on the size
> of your list, you may also want to keep a QHash<QString,
> QListWidgetItem> to keep track of the items in your list. Of course,
> this takes memory and time, so it is useless with small lists.
Thanks- that function looks to be what I was looking for. I was just
looking a level too high in the documentation. As for an in-place
update, the more I think about it, the more doable it seems. The issue
here is that the contents of the list are retrieved from a database,
so in order to know what has changed I have to retrieve the entire
list from the database. When I first wrote the routine, it just seemed
more straight-forward and simple to simply clear the display list and
re-populate it with the new list pulled from the database, rather than
trying to compare the two and update the local list. I wasn't thinking
about saving the selection at that time. And yes, I know about using
something like a QListView to display a database model directly, but I
need to add a couple of custom items to the list as well, without
affecting the database, and this didn't appear to be easily doable
using the model/view paradigm. Course, now that I think about it some
more, perhaps that approach deserves a second look as well. Although I
don't know that it would be any better about saving the selection when
updated than the current approach. Thanks again!
-----------------------------------------------
Israel Brewster
Computer Support Technician
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------
>
>
> André
>
>
> André
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
--
[ signature omitted ]