Skip to main content

TreeView

#include "toolkit/tree_view.hpp"

A scrollable, expandable/collapsible tree bound to a TreeModel, with single/multi selection. Inherits ScrollableWidget.

TreeView screenshot

TreeModel​

struct TreeNode {
std::string text;
std::vector<TreeNode> children;
bool expanded = false;
int parent_index = -1;
int depth = 0;
};

class TreeModel {
public:
virtual int root_count() const = 0;
virtual TreeNode const &root_at(int index) const = 0;
std::function<void()> on_data_changed;
};

SimpleTreeModel is a ready-made TreeModel over std::vector<TreeNode> (set_roots(), clear()). FileTreeModel is another built-in implementation, backed by the filesystem.

Example​

auto model = std::make_shared<toolkit::SimpleTreeModel>(build_tree());
auto tree = std::make_unique<toolkit::TreeView>(model);

Construction​

explicit TreeView(std::shared_ptr<TreeModel> model);

API​

MemberDescription
set_model(std::shared_ptr<TreeModel>) / model()Data source
expand(int node_index) / collapse(int node_index) / toggle(int node_index)Expansion state
set_selected_node(int) / selected_node()Single-selection cursor
set_selection(std::set<int>) / selection()Full selection set
clear_selection() / is_selected(int)Manage selection
set_multi_select(bool) / multi_select()Allow selecting multiple nodes
set_alternating_row_colors(bool) / alternating_row_colors()Zebra-striped rows

Node indices are into the view's internal flattened (visible) node list, not the model's tree structure directly.

Callbacks​

CallbackDescription
on_selection_changed — std::function<void(int node_index)>Fired when selection changes
on_node_expanded — std::function<void(int node_index)>Fired when a node is expanded/collapsed