Append more items at once along with client data 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
Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Append more items at once along with client data

Post by Marcus Frenkel »

Hi,

I'm trying to make use of the following function without success: int wxItemContainer::Append(const wxArrayString & items, wxClientData **clientData)
http://docs.wxwidgets.org/trunk/classwx ... 45778e13f3

If I want to add one item to wxSimpleHtmlListBox along with client data I use:
htmlListBox->Append(string, new wxStringClientData( shadow_string) );

How do I append wxArrayString along with Client Data?

I tried this way:

Code: Select all

wxStringClientData *StringClientData[5000];
for (;;) {
  string = ...
  shadow_string = ...

  ArrayString.Add(string);
  StringClientData[i] = new wxStringClientData(shadow_string);
}

htmlListBox->Append(ArrayString, StringClientData);
Compailer cannot match the right function and complains with this error:
'int wxItemContainer::Append(const wxString &,void *)' : cannot convert parameter 1 from 'wxArrayString' to 'const wxString &'
Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel »

Okay somehow changing

wxStringClientData *StringClientData[5000];
to
wxClientData *StringClientData[5000];

Made it compiled fine and it seems to work all right I hope it will not end up in some undefined behavior.

Another question on this, how do I set the size of wxStringClientData *StringClientData array according to the size of wxArrayString?

The following doesn't compile:
size_t PtrArraySize = BaseArrayString.Count();
wxClientData *StringClientData[PtrArraySize];
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

You can't do this, you'll have to allocate this way :

Code: Select all

wxClientData *StringClientData = new StringClientData [PtrArraySize];
Don't forget to delete[] it when yon don't need it anymore.
Jérémie
Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel »

jfouche wrote:You can't do this, you'll have to allocate this way :

Code: Select all

wxClientData *StringClientData = new StringClientData [PtrArraySize];
Don't forget to delete[] it when yon don't need it anymore.
How do you assign a value to an element in the array in that case?

About deleting the object, if you assign wxClientData then the container should delete it itself as far as I check in the source.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Code: Select all

StringClientData *StringClientData = new StringClientData [PtrArraySize];
StringClientData[0].SetFoo(x);
StringClientData[1].SetFoo(y);
StringClientData[2].SetFoo(z);
// ...
The exact name to call depends on the StringClientData class, of course
Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel »

Auria wrote:

Code: Select all

StringClientData *StringClientData = new StringClientData [PtrArraySize];
StringClientData[0].SetFoo(x);
StringClientData[1].SetFoo(y);
StringClientData[2].SetFoo(z);
// ...
The exact name to call depends on the StringClientData class, of course
The class that I want to use is wxStringClientData. I want to append wxArrayString along with array of wxStringClientData to a wxSimpleHtmlListBox. If I try with:

Code: Select all

wxClientData *StringClientData = new wxStringClientData [5000];
then I cannot use the wxStringClientData::SetData function, because StringClientData[0]. has no access to wxStringClientData but only to wxClientData.


I also tried this way:

Code: Select all

wxStringClientData **StringClientData = new wxStringClientData* [5000];
StringClientData[i]->SetData(token); //works fine
SimpleHtmlList->Append(ArrayString,StringClientData); //compilation error: 'int wxItemContainer::Append(const wxString &,void *)' : cannot convert parameter 1 from 'wxArrayString' to 'const wxString &'
Still no success.
Last edited by Marcus Frenkel on Thu Sep 30, 2010 11:18 am, edited 1 time in total.
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

why don't you use :

Code: Select all

wxStringClientData* StringClientData = new wxStringClientData [5000];
as wxStringClientData inherit wxClientData, there is no problem at all.

You can add it to you list box :

Code: Select all

wxStringClientData* clientDatas = new wxStringClientData[count];
...
for (size_t i = 0; i < count; ++i) {
   m_myHtmlSimpleList->Append(label[i], &clientData[i]);
}
Jérémie
Marcus Frenkel
Experienced Solver
Experienced Solver
Posts: 79
Joined: Thu Sep 25, 2008 12:14 am

Post by Marcus Frenkel »

jfouche wrote:why don't you use :

Code: Select all

wxStringClientData* StringClientData = new wxStringClientData [5000];
as wxStringClientData inherit wxClientData, there is no problem at all.

You can add it to you list box :

Code: Select all

wxStringClientData* clientDatas = new wxStringClientData[count];
...
for (size_t i = 0; i < count; ++i) {
   m_myHtmlSimpleList->Append(label[i], &clientData[i]);
}
I want to add all items (5000 or more) at once, if they are added one by one the insertation speed is decreased a lot.
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

OK, I understand : the API doesn't provide you a way to add multiple items with client data in only one method. As you have lot's of data, I suggest you to switch to wxHtmlListBox, which is better suitable vary a big quantity of datas.

Code: Select all

class MyListBox : public wxHtmlListBox
{
   std::vector<MyData> m_datas;

public:
   MyListBox(wxWindow*parent, ...)
   : wxHtmlListBox(...)
   {
      // Fill your datas
      for (size_t i = 0; i < ?; ++i) {
         m_datas.push_back(MyData(...));
      }
   }

   wxString OnGetItem(size_t n) const
   {
      return wxString::Format(wxT("<p>content : <b>%s</b></p>"), m_data[i].GetString().c_str());
   }
}
Jérémie
Post Reply