Up to date

This page is up to date for Godot 4.0. If you still find outdated information, please open an issue.

Tree

Inherits: Control < CanvasItem < Node < Object

A control used to show a set of internal TreeItems in a hierarchical structure.

Description

A control used to show a set of internal TreeItems in a hierarchical structure. The tree items can be selected, expanded and collapsed. The tree can have multiple columns with custom controls like LineEdits, buttons and popups. It can be useful for structured displays and interactions.

Trees are built via code, using TreeItem objects to create the structure. They have a single root, but multiple roots can be simulated with hide_root:

func _ready():
    var tree = Tree.new()
    var root = tree.create_item()
    tree.hide_root = true
    var child1 = tree.create_item(root)
    var child2 = tree.create_item(root)
    var subchild1 = tree.create_item(child1)
    subchild1.set_text(0, "Subchild1")

To iterate over all the TreeItem objects in a Tree object, use TreeItem.get_next and TreeItem.get_first_child after getting the root through get_root. You can use Object.free on a TreeItem to remove it from the Tree.

Incremental search: Like ItemList and PopupMenu, Tree supports searching within the list while the control is focused. Press a key that matches the first letter of an item's name to select the first item starting with the given letter. After that point, there are two ways to perform incremental search: 1) Press the same key again before the timeout duration to select the next item starting with the same letter. 2) Press letter keys that match the rest of the word before the timeout duration to match to select the item in question directly. Both of these actions will be reset to the beginning of the list if the timeout duration has passed since the last keystroke was registered. You can adjust the timeout duration by changing ProjectSettings.gui/timers/incremental_search_max_interval_msec.

Properties

bool

allow_reselect

false

bool

allow_rmb_select

false

bool

clip_contents

true (overrides Control)

bool

column_titles_visible

false

int

columns

1

int

drop_mode_flags

0

bool

enable_recursive_folding

true

FocusMode

focus_mode

2 (overrides Control)

bool

hide_folding

false

bool

hide_root

false

bool

scroll_horizontal_enabled

true

bool

scroll_vertical_enabled

true

SelectMode

select_mode

0

Methods

void

clear ( )

TreeItem

create_item ( TreeItem parent=null, int index=-1 )

void

deselect_all ( )

bool

edit_selected ( )

void

ensure_cursor_is_visible ( )

int

get_button_id_at_position ( Vector2 position ) const

int

get_column_at_position ( Vector2 position ) const

int

get_column_expand_ratio ( int column ) const

String

get_column_title ( int column ) const

TextDirection

get_column_title_direction ( int column ) const

String

get_column_title_language ( int column ) const

int

get_column_width ( int column ) const

Rect2

get_custom_popup_rect ( ) const

int

get_drop_section_at_position ( Vector2 position ) const

TreeItem

get_edited ( ) const

int

get_edited_column ( ) const

Rect2

get_item_area_rect ( TreeItem item, int column=-1, int button_index=-1 ) const

TreeItem

get_item_at_position ( Vector2 position ) const

TreeItem

get_next_selected ( TreeItem from )

int

get_pressed_button ( ) const

TreeItem

get_root ( ) const

Vector2

get_scroll ( ) const

TreeItem

get_selected ( ) const

int

get_selected_column ( ) const

bool

is_column_clipping_content ( int column ) const

bool

is_column_expanding ( int column ) const

void

scroll_to_item ( TreeItem item, bool center_on_item=false )

void

set_column_clip_content ( int column, bool enable )

void

set_column_custom_minimum_width ( int column, int min_width )

void

set_column_expand ( int column, bool expand )

void

set_column_expand_ratio ( int column, int ratio )

void

set_column_title ( int column, String title )

void

set_column_title_direction ( int column, TextDirection direction )

void

set_column_title_language ( int column, String language )

void

set_selected ( TreeItem item, int column )

Theme Properties

Color

children_hl_line_color

Color(0.27, 0.27, 0.27, 1)

Color

custom_button_font_highlight

Color(0.95, 0.95, 0.95, 1)

Color

drop_position_color

Color(1, 1, 1, 1)

Color

font_color

Color(0.7, 0.7, 0.7, 1)

Color

font_outline_color

Color(1, 1, 1, 1)

Color

font_selected_color

Color(1, 1, 1, 1)

Color

guide_color

Color(0.7, 0.7, 0.7, 0.25)

Color

parent_hl_line_color

Color(0.27, 0.27, 0.27, 1)

Color

relationship_line_color

Color(0.27, 0.27, 0.27, 1)

Color

title_button_color

Color(0.875, 0.875, 0.875, 1)

int

button_margin

4

int

children_hl_line_width

1

int

draw_guides

1

int

draw_relationship_lines

0

int

h_separation

4

int

item_margin

16

int

outline_size

0

int

parent_hl_line_margin

0

int

parent_hl_line_width

1

int

relationship_line_width

1

int

scroll_border

4

int

scroll_speed

12

int

v_separation

4

Font

font

Font

title_button_font

int

font_size

Texture2D

arrow

Texture2D

arrow_collapsed

Texture2D

arrow_collapsed_mirrored

Texture2D

checked

Texture2D

indeterminate

Texture2D

select_arrow

Texture2D

unchecked

Texture2D

updown

StyleBox

button_pressed

StyleBox

cursor

StyleBox

cursor_unfocused

StyleBox

custom_button

StyleBox

custom_button_hover

StyleBox

custom_button_pressed

StyleBox

focus

StyleBox

panel

StyleBox

selected

StyleBox

selected_focus

StyleBox

title_button_hover

StyleBox

title_button_normal

StyleBox

title_button_pressed


Signals

button_clicked ( TreeItem item, int column, int id, int mouse_button_index )

Emitted when a button on the tree was pressed (see TreeItem.add_button).


cell_selected ( )

Emitted when a cell is selected.


check_propagated_to_item ( TreeItem item, int column )

Emitted when TreeItem.propagate_check is called. Connect to this signal to process the items that are affected when TreeItem.propagate_check is invoked. The order that the items affected will be processed is as follows: the item that invoked the method, children of that item, and finally parents of that item.


column_title_clicked ( int column, int mouse_button_index )

Emitted when a column's title is clicked with either @GlobalScope.MOUSE_BUTTON_LEFT or @GlobalScope.MOUSE_BUTTON_RIGHT.


custom_item_clicked ( int mouse_button_index )

Emitted when an item with TreeItem.CELL_MODE_CUSTOM is clicked with a mouse button.


custom_popup_edited ( bool arrow_clicked )

Emitted when a cell with the TreeItem.CELL_MODE_CUSTOM is clicked to be edited.


empty_clicked ( Vector2 position, int mouse_button_index )

Emitted when a mouse button is clicked in the empty space of the tree.


item_activated ( )

Emitted when an item is double-clicked, or selected with a ui_accept input event (e.g. using Enter or Space on the keyboard).


item_collapsed ( TreeItem item )

Emitted when an item is collapsed by a click on the folding arrow.


item_edited ( )

Emitted when an item is edited.


item_icon_double_clicked ( )

Emitted when an item's icon is double-clicked. For a signal that emits when any part of the item is double-clicked, see item_activated.


item_mouse_selected ( Vector2 position, int mouse_button_index )

Emitted when an item is selected with a mouse button.


item_selected ( )

Emitted when an item is selected.


multi_selected ( TreeItem item, int column, bool selected )

Emitted instead of item_selected if select_mode is SELECT_MULTI.


nothing_selected ( )

Emitted when a left mouse button click does not select any item.


Enumerations

enum SelectMode:

SelectMode SELECT_SINGLE = 0

Allows selection of a single cell at a time. From the perspective of items, only a single item is allowed to be selected. And there is only one column selected in the selected item.

The focus cursor is always hidden in this mode, but it is positioned at the current selection, making the currently selected item the currently focused item.

SelectMode SELECT_ROW = 1

Allows selection of a single row at a time. From the perspective of items, only a single items is allowed to be selected. And all the columns are selected in the selected item.

The focus cursor is always hidden in this mode, but it is positioned at the first column of the current selection, making the currently selected item the currently focused item.

SelectMode SELECT_MULTI = 2

Allows selection of multiple cells at the same time. From the perspective of items, multiple items are allowed to be selected. And there can be multiple columns selected in each selected item.

The focus cursor is visible in this mode, the item or column under the cursor is not necessarily selected.