wx <--> stl 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
tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

wx <--> stl

Post by tuk1 »

Lets say we have some data in our model:

Code: Select all

std::vector<std::string> model_data;
And, we need to display the data in a list box:

Code: Select all

wxListBox* gui_listbox;

The data could be converted manually to a suitable type, by reading each: std::string, converting to: wxString, then append to: wxArrayString which in turn would append to the: wxListBox

Code: Select all

wxString mystring(std::string);
wxArrayString;
gui_listbox.Append(wxArrayString&)
But, it seems like a lot of work doing this for every transaction between model and gui.
The model could be reimplemented to use wx objects instead of stl, but clear separation between model and gui is preferable.

Is there an elegant/efficient way of dealing with 'type conversions' between stl and wx ?
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
User avatar
T-Rex
Moderator
Moderator
Posts: 1249
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: wx <--> stl

Post by T-Rex »

Use wxListCtrl with wxLC_VIRTUAL style.

From official docs:
List box elements are numbered from zero and while the maximal number of elements is unlimited, it is usually better to use a virtual control, not requiring to add all the items to it at once, such as wxDataViewCtrl or wxListCtrl with wxLC_VIRTUAL style, once more than a few hundreds items need to be displayed because this control is not optimized, neither from performance nor from user interface point of view, for large number of items.
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wx <--> stl

Post by doublemax »

One of the overloads for Append() takes a std::vector of wxString. If your wx libraries were build with USE_STL=1 (which i think is the default now),it should work out of the box.

http://docs.wxwidgets.org/trunk/classwx ... aff9a380a7
Use the source, Luke!
tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: wx <--> stl

Post by tuk1 »

doublemax wrote:One of the overloads for Append() takes a std::vector of wxString. If your wx libraries were build with USE_STL=1 (which i think is the default now),it should work out of the box.

http://docs.wxwidgets.org/trunk/classwx ... aff9a380a7
It complains 'none of the overloads match the args', and looking through the 9 overloads available via the listbox object, none take a std::vector arg.

Is there a way to check build is USE_STL=1 ?



e2a:
C:\wx\wxWidgets-3.0.3\include\wx\msw
setup.h
56kb

Assuming that is the right setup file? ..it says:
// ----------------------------------------------------------------------------
// Interoperability with the standard library.
// ----------------------------------------------------------------------------

// Set wxUSE_STL to 1 to enable maximal interoperability with the standard
// library, even at the cost of backwards compatibility.
//
// Default is 0
//
// Recommended setting: 0 as the options below already provide a relatively
// good level of interoperability and changing this option arguably isn't worth
// diverging from the official builds of the library.
#define wxUSE_STL 0
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wx <--> stl

Post by doublemax »

Under VS wxUSE_STD_DEFAULT should be automatically set to 1.

But for you the relevant part should be this:

Code: Select all

// Make wxString as much interchangeable with std::[w]string as possible, in
// particular allow implicit conversion of wxString to either of these classes.
// This comes at a price (or a benefit, depending on your point of view) of not
// allowing implicit conversion to "const char *" and "const wchar_t *".
//
// Because a lot of existing code relies on these conversions, this option is
// disabled by default but can be enabled for your build if you don't care
// about compatibility.
//
// Default is 0 if wxUSE_STL has its default value or 1 if it is enabled.
//
// Recommended setting: 0 to remain compatible with the official builds of
// wxWidgets.
#define wxUSE_STD_STRING_CONV_IN_WXSTRING wxUSE_STL
If you understand the impact, try setting wxUSE_STL to 1 and do a "clean" AND "rebuild" of the wx libraries and your project.
Use the source, Luke!
tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: wx <--> stl

Post by tuk1 »

doublemax wrote:One of the overloads for Append() takes a std::vector of wxString. If your wx libraries were build with USE_STL=1 (which i think is the default now),it should work out of the box.

http://docs.wxwidgets.org/trunk/classwx ... aff9a380a7
wxString and std::string are playing nice now, however the missing overload remained a mystery:
Capture.PNG
Capture.PNG (9.47 KiB) Viewed 1864 times
Then I seen it:
Capture.PNG
Capture.PNG (15.02 KiB) Viewed 1864 times
So, either I have to upgrade to 3.1.0 or write my own wx/stl type casting?
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wx <--> stl

Post by doublemax »

So, either I have to upgrade to 3.1.0 or write my own wx/stl type casting?
Sorry, i missed that. But yes, these are your options.
Use the source, Luke!
tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: wx <--> stl

Post by tuk1 »

Very helpful DM, thanks!
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
Post Reply