Up to date

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

TreeItem

继承: Object

代表 Tree 中某个项目的内部控件。

描述

Tree 控件中的单个项目。可以包含其他 TreeItem 作为子级,从而创建层级结构。还可以包含文本和按钮。TreeItem 不是 Node,在 Tree 内部使用。

要创建 TreeItem,请使用 Tree.create_itemcreate_child。要移除 TreeItem,请使用 Object.free

注意:按钮的 ID 值为 32 位,与始终为 64 位的 int 不同。取值范围为 -21474836482147483647

属性

bool

collapsed

int

custom_minimum_height

bool

disable_folding

bool

visible

方法

void

add_button ( int column, Texture2D button, int id=-1, bool disabled=false, String tooltip_text="" )

void

add_child ( TreeItem child )

void

call_recursive ( StringName method, ... ) vararg

void

clear_custom_bg_color ( int column )

void

clear_custom_color ( int column )

TreeItem

create_child ( int index=-1 )

void

deselect ( int column )

void

erase_button ( int column, int button_index )

AutowrapMode

get_autowrap_mode ( int column ) const

Texture2D

get_button ( int column, int button_index ) const

int

get_button_by_id ( int column, int id ) const

int

get_button_count ( int column ) const

int

get_button_id ( int column, int button_index ) const

String

get_button_tooltip_text ( int column, int button_index ) const

TreeCellMode

get_cell_mode ( int column ) const

TreeItem

get_child ( int index )

int

get_child_count ( )

TreeItem[]

get_children ( )

Color

get_custom_bg_color ( int column ) const

Color

get_custom_color ( int column ) const

Font

get_custom_font ( int column ) const

int

get_custom_font_size ( int column ) const

bool

get_expand_right ( int column ) const

TreeItem

get_first_child ( ) const

Texture2D

get_icon ( int column ) const

int

get_icon_max_width ( int column ) const

Color

get_icon_modulate ( int column ) const

Rect2

get_icon_region ( int column ) const

int

get_index ( )

String

get_language ( int column ) const

Variant

get_metadata ( int column ) const

TreeItem

get_next ( ) const

TreeItem

get_next_in_tree ( bool wrap=false )

TreeItem

get_next_visible ( bool wrap=false )

TreeItem

get_parent ( ) const

TreeItem

get_prev ( )

TreeItem

get_prev_in_tree ( bool wrap=false )

TreeItem

get_prev_visible ( bool wrap=false )

float

get_range ( int column ) const

Dictionary

get_range_config ( int column )

StructuredTextParser

get_structured_text_bidi_override ( int column ) const

Array

get_structured_text_bidi_override_options ( int column ) const

String

get_suffix ( int column ) const

String

get_text ( int column ) const

HorizontalAlignment

get_text_alignment ( int column ) const

TextDirection

get_text_direction ( int column ) const

OverrunBehavior

get_text_overrun_behavior ( int column ) const

String

get_tooltip_text ( int column ) const

Tree

get_tree ( ) const

bool

is_any_collapsed ( bool only_visible=false )

bool

is_button_disabled ( int column, int button_index ) const

bool

is_checked ( int column ) const

bool

is_custom_set_as_button ( int column ) const

bool

is_edit_multiline ( int column ) const

bool

is_editable ( int column )

bool

is_indeterminate ( int column ) const

bool

is_selectable ( int column ) const

bool

is_selected ( int column )

void

move_after ( TreeItem item )

void

move_before ( TreeItem item )

void

propagate_check ( int column, bool emit_signal=true )

void

remove_child ( TreeItem child )

void

select ( int column )

void

set_autowrap_mode ( int column, AutowrapMode autowrap_mode )

void

set_button ( int column, int button_index, Texture2D button )

void

set_button_color ( int column, int button_index, Color color )

void

set_button_disabled ( int column, int button_index, bool disabled )

void

set_button_tooltip_text ( int column, int button_index, String tooltip )

void

set_cell_mode ( int column, TreeCellMode mode )

void

set_checked ( int column, bool checked )

void

set_collapsed_recursive ( bool enable )

void

set_custom_as_button ( int column, bool enable )

void

set_custom_bg_color ( int column, Color color, bool just_outline=false )

void

set_custom_color ( int column, Color color )

void

set_custom_draw ( int column, Object object, StringName callback )

void

set_custom_font ( int column, Font font )

void

set_custom_font_size ( int column, int font_size )

void

set_edit_multiline ( int column, bool multiline )

void

set_editable ( int column, bool enabled )

void

set_expand_right ( int column, bool enable )

void

set_icon ( int column, Texture2D texture )

void

set_icon_max_width ( int column, int width )

void

set_icon_modulate ( int column, Color modulate )

void

set_icon_region ( int column, Rect2 region )

void

set_indeterminate ( int column, bool indeterminate )

void

set_language ( int column, String language )

void

set_metadata ( int column, Variant meta )

void

set_range ( int column, float value )

void

set_range_config ( int column, float min, float max, float step, bool expr=false )

void

set_selectable ( int column, bool selectable )

void

set_structured_text_bidi_override ( int column, StructuredTextParser parser )

void

set_structured_text_bidi_override_options ( int column, Array args )

void

set_suffix ( int column, String text )

void

set_text ( int column, String text )

void

set_text_alignment ( int column, HorizontalAlignment text_alignment )

void

set_text_direction ( int column, TextDirection direction )

void

set_text_overrun_behavior ( int column, OverrunBehavior overrun_behavior )

void

set_tooltip_text ( int column, String tooltip )

void

uncollapse_tree ( )


枚举

enum TreeCellMode:

TreeCellMode CELL_MODE_STRING = 0

单元包含字符串。

TreeCellMode CELL_MODE_CHECK = 1

单元格包含复选框。

TreeCellMode CELL_MODE_RANGE = 2

单元包含一个范围。

TreeCellMode CELL_MODE_ICON = 3

单元包含图标。

TreeCellMode CELL_MODE_CUSTOM = 4


属性说明

bool collapsed

  • void set_collapsed ( bool value )

  • bool is_collapsed ( )

如果为 true,则该 TreeItem 被折叠。


int custom_minimum_height

  • void set_custom_minimum_height ( int value )

  • int get_custom_minimum_height ( )

自定义最小高度。


bool disable_folding

  • void set_disable_folding ( bool value )

  • bool is_folding_disabled ( )

如果为 true,则这个 TreeItem 禁用折叠。


bool visible

  • void set_visible ( bool value )

  • bool is_visible ( )

如果为 true,则该 TreeItem 可见(默认)。

请注意,如果将 TreeItem 设置为不可见,则其子项也将不可见。


方法说明

void add_button ( int column, Texture2D button, int id=-1, bool disabled=false, String tooltip_text="" )

column 列添加一个带有 Texture button 的按钮。id 用于标识按钮。如果未指定,则使用下一个可用索引,可以在此方法之前调用 get_button_count 来获取该索引。另外,该按钮还可以通过 disabled 禁用、通过 tooltip_text 设置工具提示。


void add_child ( TreeItem child )

将已解除父子关系的 TreeItem 添加为这个树项的直接子项。child 树项必须不是任何 Tree 的一部分,也不能有任何 TreeItem 父级。另见 remove_child


void call_recursive ( StringName method, ... ) vararg

递归调用在实际 TreeItem 及其子项上的 method。将参数作为一个逗号分隔列表传递。


void clear_custom_bg_color ( int column )

重置指定列默认的背景颜色。


void clear_custom_color ( int column )

重置指定列默认的颜色。


TreeItem create_child ( int index=-1 )

创建项目并添加为子项。

新建的项目会插入到索引 index 位置(默认的 -1 表示最后的位置),如果 index 比子项数量大则会作为最后一项。


void deselect ( int column )

取消选择指定列。


void erase_button ( int column, int button_index )

删除列 column 中索引 button_index 处的按钮。


AutowrapMode get_autowrap_mode ( int column ) const

返回给定列 column 的文本自动换行模式。默认为 TextServer.AUTOWRAP_OFF


Texture2D get_button ( int column, int button_index ) const

返回在 column 列中索引为 button_index 的按钮的 Texture


int get_button_by_id ( int column, int id ) const

如果在 column 列中存在 ID 为 id 的按钮,则返回其索引号,否则返回 -1。


int get_button_count ( int column ) const

返回在 column 列中按钮的数量。


int get_button_id ( int column, int button_index ) const

返回在 column 列中索引为 button_index 的按钮的 ID。


String get_button_tooltip_text ( int column, int button_index ) const

返回在 column 列中索引为 button_index 的按钮的工具提示字符串。


TreeCellMode get_cell_mode ( int column ) const

返回该列的单元格模式。


TreeItem get_child ( int index )

按其 index 返回一个子项(参见 get_child_count)。该方法通常被用于迭代一个项目的所有子项。

负索引将从最后一个访问孩子。


int get_child_count ( )

返回子项的数量。


TreeItem[] get_children ( )

返回引用该项目子级的数组。


Color get_custom_bg_color ( int column ) const

返回列 column 的自定义背景色。


Color get_custom_color ( int column ) const

返回列 column 的自定义颜色。


Font get_custom_font ( int column ) const

返回用于在 column 列绘制文本的自定义字体。


int get_custom_font_size ( int column ) const

返回用于在 column 列绘制文本的自定义字体大小。


bool get_expand_right ( int column ) const

如果设置了 expand_right,则返回 true


TreeItem get_first_child ( ) const

返回该 TreeItem 的第一个子项。


Texture2D get_icon ( int column ) const

返回给定列的图标 Texture2D。如果未设置图标,则会出错。


int get_icon_max_width ( int column ) const

返回给定列 column 中图标所允许的最大宽度。


Color get_icon_modulate ( int column ) const

返回调制列的图标的 Color 颜色。


Rect2 get_icon_region ( int column ) const

返回图标 Texture2D 的区域,类型为 Rect2


int get_index ( )

返回该节点在树中的顺序。例如对第一个子项调用时,得到的位置为 0


String get_language ( int column ) const

返回项目文本的语言代码。


Variant get_metadata ( int column ) const

返回使用 set_metadata 为指定列设置的元数据。


TreeItem get_next ( ) const

返回树中的下一个兄弟 TreeItem,如果没有,则返回一个空对象。


TreeItem get_next_in_tree ( bool wrap=false )

返回树中下一个可见的同级 TreeItem(按照深度优先顺序搜索),如果不存在则返回 null 对象。

如果启用了 wrap,则当在最后一个元素调用时,该方法将环绕到树中的第一个可见元素,否则它将返回 null


TreeItem get_next_visible ( bool wrap=false )

返回树中下一个可见的同级 TreeItem(按照深度优先顺序搜索),如果不存在则返回 null 对象。

如果启用了 wrap,则当在最后一个可见元素调用时,该方法将环绕到树中的第一个可见元素,否则它将返回 null


TreeItem get_parent ( ) const

返回父级 TreeItem,如果没有,则返回一个空对象。


TreeItem get_prev ( )

返回树中的前一个兄弟 TreeItem,如果没有,则返回一个空对象。


TreeItem get_prev_in_tree ( bool wrap=false )

返回树中前一个同级 TreeItem(按照深度优先顺序搜索),如果不存在则返回 null 对象。

如果启用了 wrap,则在第一个可见元素上调用时,该方法将环绕到树中的最后一个可见元素,否则它将返回 null


TreeItem get_prev_visible ( bool wrap=false )

返回树中前一个可见的同级 TreeItem(按照深度优先顺序搜索),如果不存在则返回 null 对象。

如果启用了 wrap,则在第一个可见元素上调用时,该方法将环绕到树中的最后一个可见元素,否则它将返回 null


float get_range ( int column ) const

返回 CELL_MODE_RANGE 列的值。


Dictionary get_range_config ( int column )

返回包含给定列的范围参数的字典。键是“min”、“max”、“step”和“expr”。


StructuredTextParser get_structured_text_bidi_override ( int column ) const

目前没有这个方法的描述。请帮我们贡献一个


Array get_structured_text_bidi_override_options ( int column ) const

目前没有这个方法的描述。请帮我们贡献一个


String get_suffix ( int column ) const

获取显示在列值后面的后缀字符串。


String get_text ( int column ) const

返回给定列的文本。


HorizontalAlignment get_text_alignment ( int column ) const

返回给定列的文本对齐方式。


TextDirection get_text_direction ( int column ) const

返回项目文本的基础书写方向。


OverrunBehavior get_text_overrun_behavior ( int column ) const

返回当给定 column 中文本超出项目的边界矩形时的裁剪行为。默认情况下它是 TextServer.OVERRUN_TRIM_ELLIPSIS


String get_tooltip_text ( int column ) const

设置给定列的工具提示文本。


Tree get_tree ( ) const

返回拥有此 TreeItem 的 Tree


bool is_any_collapsed ( bool only_visible=false )

如果这个 TreeItem 或其任意子级为折叠状态,则返回 true

如果 only_visibletrue,则会忽略不可见的 TreeItem


bool is_button_disabled ( int column, int button_index ) const

如果给定列 column 上索引为 button_index 的按钮被禁用,则返回 true


bool is_checked ( int column ) const

如果给定的列 column 被勾选,则返回 true


bool is_custom_set_as_button ( int column ) const

目前没有这个方法的描述。请帮我们贡献一个


bool is_edit_multiline ( int column ) const

如果给定的列 column 可多行编辑,则返回 true


bool is_editable ( int column )

如果给定的列 column 可编辑,则返回 true


bool is_indeterminate ( int column ) const

如果给定的列 column 未确定,则返回 true


bool is_selectable ( int column ) const

如果给定的列 column 可选,则返回 true


bool is_selected ( int column )

如果给定的列 column 被选中,则返回 true


void move_after ( TreeItem item )

将这个 TreeItem 移动至给定的 item 之后。

注意:无法移动至根部,也无法移动根部。


void move_before ( TreeItem item )

将这个 TreeItem 移动至给定的 item 之前。

注意:无法移动至根部,也无法移动根部。


void propagate_check ( int column, bool emit_signal=true )

将该项的勾选状态传播给给定 column 的子项和父项。可以通过连接到 Tree.check_propagated_to_item 来处理受该方法调用影响的项目。受影响的项目的处理顺序如下:调用该方法的项目、该项的子项,最后是该项的父项。如果 emit_signalfalse,则不会发出 Tree.check_propagated_to_item


void remove_child ( TreeItem child )

将给定的子项 TreeItem 和它的所有子项从 Tree 中移除。注意,它并未从内存中释放该项,所以之后可重新使用(见 add_child)。要完全删除 TreeItem,请使用 Object.free

注意:如果你想要将一个子项移动到其他 Tree 中,则不必手动先移除再添加,你可以使用 move_beforemove_after


void select ( int column )

选中 column 指定的列。


void set_autowrap_mode ( int column, AutowrapMode autowrap_mode )

设置给定 column 的自动换行模式。如果设置为 TextServer.AUTOWRAP_OFF 以外的值,则文本将在节点的边界矩形内换行。


void set_button ( int column, int button_index, Texture2D button )

将给定列中索引为 button_index 的按钮 Texture2D 设置为 button


void set_button_color ( int column, int button_index, Color color )

将给定列中索引为 button_index 的按钮颜色设置为 color


void set_button_disabled ( int column, int button_index, bool disabled )

如果为 true,则禁用给定列 column 中索引为 button_index 的按钮。


void set_button_tooltip_text ( int column, int button_index, String tooltip )

设置给定 column 中索引 button_index 处按钮的工具提示文本。


void set_cell_mode ( int column, TreeCellMode mode )

将给定列的单元格模式设置为 mode。见 TreeCellMode 常量。


void set_checked ( int column, bool checked )

如果 checkedtrue,则给定列 column 处于勾选状态。会清空该列的中间状态。


void set_collapsed_recursive ( bool enable )

折叠或展开该 TreeItem 及该项的所有子级。


void set_custom_as_button ( int column, bool enable )

目前没有这个方法的描述。请帮我们贡献一个


void set_custom_bg_color ( int column, Color color, bool just_outline=false )

设置给定列的自定义背景颜色,以及是否只将其作为一个轮廓。


void set_custom_color ( int column, Color color )

设置给定列的自定义颜色。


void set_custom_draw ( int column, Object object, StringName callback )

将给定列的自定义绘制回调设置为 object 上的 callback 方法。

callback 应该接受两个参数:被绘制的 TreeItem 及其作为一个 Rect2 的位置和大小。


void set_custom_font ( int column, Font font )

设置用于在给定列 column 中绘制文本的自定义字体。


void set_custom_font_size ( int column, int font_size )

设置用于在给定列 column 中绘制文本的自定义字体大小。


void set_edit_multiline ( int column, bool multiline )

如果 multilinetrue,则给定的列 column 可进行多行编辑。

注意:这个选项仅影响编辑该列时所出现控件的类型(LineEditTextEdit)。即便该列不可进行多行编辑,也可以通过 set_text 来设置多行的值。


void set_editable ( int column, bool enabled )

如果 enabledtrue,则给定的列 column 可编辑。


void set_expand_right ( int column, bool enable )

如果 enabletrue,则给定的列 column 向右扩展。


void set_icon ( int column, Texture2D texture )

设置给定列的图标 Texture2D


void set_icon_max_width ( int column, int width )

设置给定列 column 所允许的最大图标宽度。这是在图标默认大小和 Tree.icon_max_width 的基础上的限制。高度会根据图标的长宽比调整。


void set_icon_modulate ( int column, Color modulate )

modulate 调制给定列的图标。


void set_icon_region ( int column, Rect2 region )

设置给定列的图标的纹理区域。


void set_indeterminate ( int column, bool indeterminate )

如果 indeterminatetrue,则给定列 column 被标记为未决状态。

注意:如果从 false 设置为 true,则该列的勾选状态会被清除。


void set_language ( int column, String language )

设置项目文本的语言代码,用于断行和文本塑形算法,如果留空则使用当前区域设置。


void set_metadata ( int column, Variant meta )

设置给定列的元数据,之后可用 get_metadata 进行检索。例如,这可用于存储对原始数据的引用。


void set_range ( int column, float value )

设置 CELL_MODE_RANGE 列的值。


void set_range_config ( int column, float min, float max, float step, bool expr=false )

设置列的可接受值的范围。该列必须处于 CELL_MODE_RANGE 模式。

如果 exprtrue,则编辑模式滑块将使用与 Range.exp_edit 一样的指数刻度。


void set_selectable ( int column, bool selectable )

如果 selectabletrue,则给定列 column 可选。


void set_structured_text_bidi_override ( int column, StructuredTextParser parser )

目前没有这个方法的描述。请帮我们贡献一个


void set_structured_text_bidi_override_options ( int column, Array args )

目前没有这个方法的描述。请帮我们贡献一个


void set_suffix ( int column, String text )

设置字符串,显示在列的值之后(例如,单位的缩写)。


void set_text ( int column, String text )

设置给定列的文本值。


void set_text_alignment ( int column, HorizontalAlignment text_alignment )

设置给定列的文本对齐方式。可能的值见 HorizontalAlignment


void set_text_direction ( int column, TextDirection direction )

设置项目文本的基础书写方向。


void set_text_overrun_behavior ( int column, OverrunBehavior overrun_behavior )

设置当文本超出给定 column 中项目的边界矩形时的裁剪行为。


void set_tooltip_text ( int column, String tooltip )

设置给定列的工具提示文本。


void uncollapse_tree ( )

目前没有这个方法的描述。请帮我们贡献一个