How to share strings between many combo boxes? 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: 147
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

How to share strings between many combo boxes?

Post by Tapsa » Fri Sep 30, 2016 2:46 pm

I need dozens of combo boxes around my program to śhare one list of item texts.
Currently my combo boxes are wxOwnerDrawnComboBox and I use Append on each of them, so in memory I have same identical list of strings dozens of times. How could I make my combo boxes only use one list of strings to display texts?

I could overwrite the OnDrawItem but I don't want to handle proper font, font size, front and back color etc.

Qt equivalent of what I need is QAbstractListModel.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5026
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to share strings between many combo boxes?

Post by ONEEYEMAN » Fri Sep 30, 2016 4:09 pm

Hi,
This is more of the basic C++ questions rather than wxWidgets.

Just create a class whose parent will be wxOwnerDrawComboBox and use it in you program.

Thank you.

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

Re: How to share strings between many combo boxes?

Post by Tapsa » Sat Oct 01, 2016 7:13 am

-.- very helpful answer

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

Re: How to share strings between many combo boxes?

Post by doublemax » Sat Oct 01, 2016 8:01 am

wxItemContainer just doesn't support virtual data. If it's really about saving memory (which i doubt), then you could only fill the control with empty strings, override OnDrawItem and then get the "real" string data from another source based on the item number.

As for the drawing, wxRendererNative::DrawItemText() could probably handle most of the dirty work:
http://docs.wxwidgets.org/trunk/classwx ... ac0e53fe3e
Use the source, Luke!

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

Re: How to share strings between many combo boxes?

Post by Tapsa » Sat Oct 01, 2016 2:25 pm

Thank you doublemax. So there is no wxListView kind of combo box that lets me tell what to write from a function like OnGetItemText.

I came up with this piece of code where I can write any string and make it look good.

Code: Select all

void AGEComboBox::OnDrawItem(wxDC &dc, const wxRect &rect, int item, int flags) const
{
    wxRendererNative::Get().DrawItemText(const_cast<AGEComboBox*>(this), dc, "Terve", rect, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
}

Post Reply