Better way to refresh ListCtrl/ListView items Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Better way to refresh ListCtrl/ListView items

Post by cneng3 »

I have a std::set to hold some data that I want to display in a ListCtrl, and this std::set is constantly being modified so every time it gets modified, I need to sort it and refresh the ListCtrl. The current way I have is something like this

Code: Select all

ListCtrl * myList;

void refreshList(const std::set& mySet)
{
	/// remove all current items
	myList->DeleteAllItems();
	
	/// re-add every single items 
	for(const auto& item : mySet)
	{
		/// code that adds the item to the list
	}
}
The problem I have is that I think the way I have is very dumb since it always deletes all items and re-add all items in the set. I feel like this is very inefficient on a large scale. So I'm wondering if there are some better ways to do this.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Better way to refresh ListCtrl/ListView items

Post by PB »

If your list happens to be displayed only in the report, what about using wxLC_VIRTUAL? Otherwise there is probably not much to do.

Its lack of elegance aside, Is there an actual issue (performance etc.) with the current implementation?
cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Re: Better way to refresh ListCtrl/ListView items

Post by cneng3 »

PB wrote:If your list happens to be displayed only in the report, what about using wxLC_VIRTUAL? Otherwise, there is probably not much to do.

Its lack of elegance aside, Is there an actual issue (performance etc.) with the current implementation?
Hi, I'm using wxLC_REPORT style. I just read the documentation and it says "The application provides items text on demand" about wxLC_VIRTUAL. I'm not too sure about what it means. Could you explain more about this to me? Thank you :D
I believe(so far it could be just a theory) that with the current way I have, performance will be an issue because eventually I will use data on a large scale, let's say 2000. So every time I refresh my list, my application would loop 2000 times to add each item to the list, and this could happen multiple times within a short time period.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Better way to refresh ListCtrl/ListView items

Post by doublemax »

"The application provides items text on demand" about wxLC_VIRTUAL. I'm not too sure about what it means.
viewtopic.php?p=172585#p172585

This mentions a database as data source, but the principle is the same.
Use the source, Luke!
thoray
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sun Oct 18, 2015 9:31 am

Re: Better way to refresh ListCtrl/ListView items

Post by thoray »

If the number of items stays constant you could instead do this:

Code: Select all

for (int 0=;i<N_items;i++)
	{
        m_list->SetItem(i,col0_id,newval1);
        m_list->SetItem(i,col1_id,newval2);
        }
Or if only a few items change you could run the above code for only the changed ids.

I haven't tried refreshes yet when removing or adding items.
cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Re: Better way to refresh ListCtrl/ListView items

Post by cneng3 »

doublemax wrote:
"The application provides items text on demand" about wxLC_VIRTUAL. I'm not too sure about what it means.
viewtopic.php?p=172585#p172585

This mentions a database as data source, but the principle is the same.
So if I understand correctly, by making the listCtrl 'virtual' means making it dynamic, because it only prints out the text on demand. So I don't really need to manually refresh my list no more since it "refreshes" itself
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Better way to refresh ListCtrl/ListView items

Post by doublemax »

You still need to call Refresh() when the data changes as the control itself has no knowledge about that.
Use the source, Luke!
cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Re: Better way to refresh ListCtrl/ListView items

Post by cneng3 »

doublemax wrote:You still need to call Refresh() when the data changes as the control itself has no knowledge about that.
Thank you so much!`
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Better way to refresh ListCtrl/ListView items

Post by PB »

BTW, wxListCtrl in the virtual mode has RefreshItems(), in case you would need to refresh only a subset of items.

BTW2, I hope it was understood but just to be sure: you also need to tell the list how many items are there with SetItemCount().

BTW3, the methods relevant for the virtual mode are listed at the beginning of wxListCtrl docs.
cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Re: Better way to refresh ListCtrl/ListView items

Post by cneng3 »

PB wrote:BTW, wxListCtrl in the virtual mode has RefreshItems(), in case you would need to refresh only a subset of items.

BTW2, I hope it was understood but just to be sure: you also need to tell the list how many items are there with SetItemCount().

BTW3, the methods relevant for the virtual mode are listed at the beginning of wxListCtrl docs.
Thank you for all the tips. I'm aware of the usage of SetIemCount(). I've got a very basic virtual list work.
Post Reply