{"id":4636,"date":"2025-01-27T10:58:54","date_gmt":"2025-01-27T10:58:54","guid":{"rendered":"https:\/\/truelogic.org\/wordpress\/?p=4636"},"modified":"2025-01-27T10:58:54","modified_gmt":"2025-01-27T10:58:54","slug":"18-wxwidgets-wxtreectrl","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2025\/01\/27\/18-wxwidgets-wxtreectrl\/","title":{"rendered":"18.wxWidgets &#8211; wxTreeCtrl"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>The wxTreeCtrl lets you display data in a hierarchical tree format. It supports different styles and has comprehensive events which can be processed. Given below is a sample tree which shows cities in countries. <\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<p>tree.h<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n#include &lt;wx\/wx.h&gt;\n#endif \/\/ WX_PRECOMP\n\n#include &lt;wx\/treectrl.h&gt;\n\nclass Tree: public wxFrame {\npublic:\n    Tree(const wxString &amp;title);\nprivate:\n    wxTreeCtrl *m_tree;\n    wxStaticText *m_title;\n    void treeSelChanged(wxTreeEvent &amp;evt);\n};\n\n<\/pre><\/div>\n\n\n<p>tree.cpp<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;tree.h&quot;\n\nTree::Tree(const wxString &amp;title):\n    wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(800,400)) {\n\n    wxPanel *panHome = new wxPanel(this, -1);\n    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);\n    panHome-&gt;SetSizer(vbox);\n\n    m_title = new wxStaticText(panHome, -1, wxT(&quot;Tree Demo&quot;));\n    vbox-&gt;Add(m_title, 0, wxEXPAND | wxALL,2);\n    m_tree = new wxTreeCtrl(panHome, -1, wxDefaultPosition, wxDefaultSize, wxTR_HAS_BUTTONS | wxTR_SINGLE,\n                            wxDefaultValidator, wxT(&quot;Tree Control&quot;));\n    vbox-&gt;Add(m_tree, 4, wxEXPAND | wxALL, 2);\n\n    \/\/populate tree\n    wxTreeItemId root = m_tree-&gt;AddRoot(wxT(&quot;Countries&quot;), -1, -1, NULL);\n    wxTreeItemId uk = m_tree-&gt;AppendItem(root, wxT(&quot;United Kingdom&quot;), -1, -1, NULL);\n    wxTreeItemId uk_london = m_tree-&gt;AppendItem(uk, wxT(&quot;London&quot;), -1, -1, NULL);\n    wxTreeItemId uk_manchester = m_tree-&gt;AppendItem(uk, wxT(&quot;Manchester&quot;), -1, -1, NULL);\n    wxTreeItemId uk_birmingham= m_tree-&gt;AppendItem(uk, wxT(&quot;Birmingham&quot;), -1, -1, NULL);\n\n    wxTreeItemId usa = m_tree-&gt;AppendItem(root, wxT(&quot;United States&quot;), -1, -1, NULL);\n    wxTreeItemId usa_ny = m_tree-&gt;AppendItem(usa, wxT(&quot;New York&quot;), -1, -1, NULL);\n    wxTreeItemId usa_chi = m_tree-&gt;AppendItem(usa, wxT(&quot;Chicago&quot;), -1, -1, NULL);\n    wxTreeItemId usa_det = m_tree-&gt;AppendItem(usa, wxT(&quot;Detroit&quot;), -1, -1, NULL);\n    wxTreeItemId usa_wash = m_tree-&gt;AppendItem(usa, wxT(&quot;Washington&quot;), -1, -1, NULL);\n\n    wxTreeItemId russia = m_tree-&gt;AppendItem(root, wxT(&quot;Russia&quot;), -1, -1, NULL);\n    wxTreeItemId russia_moscow = m_tree-&gt;AppendItem(russia, wxT(&quot;Moscow&quot;), -1, -1, NULL);\n    wxTreeItemId russia_lenin = m_tree-&gt;AppendItem(russia, wxT(&quot;Leningrad&quot;), -1, -1, NULL);\n\n    wxTreeItemId italy = m_tree-&gt;AppendItem(root, wxT(&quot;Italy&quot;), -1, -1, NULL);\n    wxTreeItemId italy_milan = m_tree-&gt;AppendItem(italy, wxT(&quot;Milan&quot;), -1, -1, NULL);\n    wxTreeItemId italy_rome = m_tree-&gt;AppendItem(italy, wxT(&quot;Rome&quot;), -1, -1, NULL);\n    wxTreeItemId italy_pisa = m_tree-&gt;AppendItem(italy, wxT(&quot;Pisa&quot;), -1, -1, NULL);\n\n    m_tree-&gt;Bind(wxEVT_TREE_SEL_CHANGED, &amp;Tree::treeSelChanged, this);\n}\n\nvoid Tree::treeSelChanged(wxTreeEvent &amp;evt) {\n    wxTreeItemId item = evt.GetItem();\n    wxString lbl = m_tree-&gt;GetItemText(item);\n    wxMessageBox(lbl);\n}\n\n\n<\/pre><\/div>\n\n\n<p>main.h<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n#include &lt;wx\/wx.h&gt;\n#endif \/\/ WX_PRECOMP\n\n#include &quot;tree.h&quot;\n\nclass MainApp: public wxApp {\npublic:\n    virtual bool OnInit();\n\n};\n\n\n\n\n<\/pre><\/div>\n\n\n<p>main.cpp<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n\nbool MainApp::OnInit() {\n    Tree *tree = new Tree(wxT(&quot;Tree Demo App&quot;));\n    tree-&gt;Show(true);\n    return TRUE;\n}\n\nwxIMPLEMENT_APP(MainApp);\n\n<\/pre><\/div>\n\n\n<p>The sample output is shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"751\" height=\"351\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/Screenshot-from-2025-01-27-16-28-07.png\" alt=\"\" class=\"wp-image-4642\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/Screenshot-from-2025-01-27-16-28-07.png 751w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/Screenshot-from-2025-01-27-16-28-07-620x290.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/Screenshot-from-2025-01-27-16-28-07-300x140.png 300w\" sizes=\"auto, (max-width: 751px) 100vw, 751px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW The wxTreeCtrl lets you display data in a hierarchical tree format. It supports different styles and has comprehensive events which can be processed. Given <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2025\/01\/27\/18-wxwidgets-wxtreectrl\/\" title=\"18.wxWidgets &#8211; wxTreeCtrl\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":4405,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4636","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4636"}],"version-history":[{"count":6,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4636\/revisions"}],"predecessor-version":[{"id":4643,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4636\/revisions\/4643"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/4405"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}