wxtreectrl item id and label 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
User avatar
luna80
In need of some credit
In need of some credit
Posts: 5
Joined: Mon Apr 05, 2021 6:53 am

wxtreectrl item id and label

Post by luna80 »

Hi to all.
I have a question: how can I set an item ID to item to add and get it when selected?
I want add an object got from a database and this object has an id and a label, I want show the label and get the id when selected.
I'm very sorry for my english but I hope you can understand me

thanks a lot in advance
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxtreectrl item id and label

Post by doublemax »

Derive your own class from wxTreeItemData, in the subclass you can add member variables to store additional information. Then use wxTreeCtrl::SetItemData() to assign it to an item.
Check the "treectrl" sample that comes with wxWidgets, look for occurrences of "MyTreeItemData".
https://docs.wxwidgets.org/trunk/classw ... 11bb4b3b46
Use the source, Luke!
User avatar
luna80
In need of some credit
In need of some credit
Posts: 5
Joined: Mon Apr 05, 2021 6:53 am

Re: wxtreectrl item id and label

Post by luna80 »

Oh!
Thanks a lot!
User avatar
luna80
In need of some credit
In need of some credit
Posts: 5
Joined: Mon Apr 05, 2021 6:53 am

Re: wxtreectrl item id and label

Post by luna80 »

for those are interested...

Code: Select all

class TreeObject : public wxTreeItemData
{
	
	private:
	  wxString label;
	  long long id;
	  long long idTipo;
	  
	public:
	  wxString GetLabel(){ return label;}
	  void SetLabel(wxString label){ this->label = label;}
	  long long GetId(){ return id;}
	  void SetId(long long id){ this->id = id;}
	  long long GetIdTipo(){ return idTipo;}
	  void SetIdTipo(long long id){ this->idTipo = id;}
};

Code: Select all

void ReportSettOggettiDialog::OnClickTree(wxTreeEvent& event)
{
	wxTreeItemId selezione = event.GetItem();
	TreeObject* item = (TreeObject*)tree->GetItemData(selezione);
	cout <<  item->GetIdTipo() << ", " <<  item->GetId() << ", "<<  item->GetLabel() << endl;

}
Post Reply