How to add multiple void* data into wxListBox? 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
Tapsa
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

How to add multiple void* data into wxListBox?

Post by Tapsa » Mon Dec 16, 2013 5:15 pm

I know how to add wxArrayString into wxListBox with single Append but I haven't figured out how I can add custom data at the same time.

This is how I add custom data with string one by one:
myWxListBox->Append(myString, (void*)&myVariableInVector);

How to preadd custom data into void** (no C malloc stuff etc) and then add it at once into wxListBox?
I cannot simply call myWxListBox->Append(myWxArrayString, myVector);
Appends several items at once into the control.
Notice that calling this method is usually much faster than appending them one by one if you need to add a lot of items.
I have about 11 000 items to add and the list needs to be refreshed repeatedly as the shown items change.

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax » Mon Dec 16, 2013 5:35 pm

As you already noticed yourself, there is no method to add multiple entries together with client data.

However, for that number of items, especially if they're dynamic, you should always use a virtual control. In this case, i'd suggest a virtual wxListCtrl. Without header and with only one column, it would look almost the same as a wxListBox.
Use the source, Luke!

Tapsa
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: How to add multiple void* data into wxListBox?

Post by Tapsa » Mon Dec 16, 2013 6:10 pm

:( Thanks for the suggestion. Sounds a bit complicated...
So would myWxListBox->GetClientData(integer) still work?
Why do I need to override useless OnGetItemText?

Anyhow, it appears that assigning data later in a for loop and appending all strings as wxArrayString decreases the overall time by ~30 %.

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax » Mon Dec 16, 2013 6:19 pm

Thanks for the suggestion. Sounds a bit complicated.
Not really. The idea is that you don't put any data into the control. You just tell the control how many entries it has. Then you subclass wxListCtrl and override wxListCtrl::OnGetItemText(). For each row/column that needs to be displayed, this methods will be called and you just return the string.

It's ideal if you already have all the data in memory. And i assume that's the case for you. And as you're working on your own dataset, anything like SetClientData() is not necessary (and wouldn't work for a virtual control anyway).
Use the source, Luke!

Tapsa
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: How to add multiple void* data into wxListBox?

Post by Tapsa » Mon Dec 16, 2013 7:59 pm

It seems that wxListCtrl doesn't have GetSelections(), SetSelection(), SetFirstItem().
For example, in a multiple selection virtual list control, the selections won't be sent when many items are selected at once because this could mean iterating over all the items.
Yes, I already have all the data in memory.
Maybe I could hide integers (position in vector) behind \0 in wxString.

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax » Mon Dec 16, 2013 10:07 pm

It seems that wxListCtrl doesn't have GetSelections(), SetSelection(), SetFirstItem().
The API for wxListCtrl is different, but this is all possible. Check the "listctrl" sample that comes with wxWIdgets. Alternatively check wxListView which is a simple wrapper around wxListCtrl.
http://docs.wxwidgets.org/trunk/classwx_list_view.html
Yes, I already have all the data in memory.
Maybe I could hide integers (position in vector) behind \0 in wxString.
No. You don't need to store any client data. When OnGetItemText() gets called, you get "item" as parameter. This is the index into your (internal) dat and this should be all you need.
Use the source, Luke!

Tapsa
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: How to add multiple void* data into wxListBox?

Post by Tapsa » Tue Dec 17, 2013 3:26 pm

With wxListBox I can select multiple items by holding mouse left button down. Is it possible with custom wxListView?
I also need only one column which should always be the size of the widest item string.
Custom wxListView seems to be insanely faster than wxListBox :)

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax » Tue Dec 17, 2013 6:48 pm

With wxListBox I can select multiple items by holding mouse left button down. Is it possible with custom wxListView
Yes, by *not* setting the wxLC_SINGLE_SEL style flag when creating the control. (Multiple selection is the default)
Use the source, Luke!

Tapsa
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: How to add multiple void* data into wxListBox?

Post by Tapsa » Wed Dec 18, 2013 4:49 pm

I have not set wxLC_SINGLE_SEL. I use this:
wxListView(parent, wxID_ANY, wxDefaultPosition, size, wxLC_VIRTUAL | wxLC_REPORT | wxLC_NO_HEADER)

Without mouse drag selection selecting many at once becomes a lot harder (needs keyboard input).

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax » Wed Dec 18, 2013 4:59 pm

Which platform and wxWidgets version are you using?

Check the "listctrl" sample, press F7 to test a virtual view. Multiple selection with mouse and keyboard works fine there for me.
Use the source, Luke!

Tapsa
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: How to add multiple void* data into wxListBox?

Post by Tapsa » Wed Dec 18, 2013 5:01 pm

Well, I don't mean multiple selecting like it is in that sample... :(
wxMSW and 3.0.0.

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax » Wed Dec 18, 2013 5:14 pm

Well, I don't mean multiple selecting like it is in that sample
What do you mean then?
Use the source, Luke!

Tapsa
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: How to add multiple void* data into wxListBox?

Post by Tapsa » Wed Dec 18, 2013 5:31 pm

Multiple selecting with single mouse click/drag.

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax » Wed Dec 18, 2013 5:43 pm

So you mean the wxLB_EXTENDED style for wxListBox. Seems that wxListCtrl does not support that.
Use the source, Luke!

Post Reply