easymodel.treemodel.TreeItem

class easymodel.treemodel.TreeItem(data, parent=None)[source]

Bases: object

General TreeItem

You can represent a tree structure with these tree items. Each item should contain some data that it can give to the model. Note that each tree always has one root item. Even if you have multiple top level items, they are all grouped under one root. The data for the root item can be None but it is advised to use a ListItemData so you can provide horizontal headers.

TreeItems should always belong to only one model. Once a new TreeModel gets initialized all TreeItems will share the same model. When you add a new Item or delete one, the model gets automatically updated. You do not need to call TreeModel insertRow or removeRow. Just use add_child, remove_child or create a new TreeItem and provide a parent item to the constructor.

Initialize a new TreeItem that holds some data and might be parented under parent

The child count will be zero. Will automatically set the parent and update the model if the parent is not None.

Parameters:
  • data (ItemData) – the data item. if the tree item is the root, the data will be used for horizontal headers! It is recommended to use ListItemData in that case.
  • parent (TreeItem) – the parent treeitem
Raises:

None

__init__(data, parent=None)[source]

Initialize a new TreeItem that holds some data and might be parented under parent

The child count will be zero. Will automatically set the parent and update the model if the parent is not None.

Parameters:
  • data (ItemData) – the data item. if the tree item is the root, the data will be used for horizontal headers! It is recommended to use ListItemData in that case.
  • parent (TreeItem) – the parent treeitem
Raises:

None

Methods

__init__(data[, parent]) Initialize a new TreeItem that holds some data and might be parented under parent
add_child(child) Add child to children of this TreeItem
child(row) Return the child at the specified row
child_count() Return the number of children
column_count() Return the number of columns that the children have
data(column, role) Return the data for the column and role
flags(index) Return the flags for the item
get_model() Return the model the item belongs to
internal_data() Return the internal data of the item data
itemdata() Return the internal ItemData
parent() Return the parent tree item
remove_child(child) Remove the child from this TreeItem
row() Return the index of this tree item in the parent rows
set_data(column, value, role) Set the data of column to value
set_model(model) Set the model the item belongs to
set_parent(parent) Set the parent of the treeitem
to_index([column]) Return the index for this tree item in the model
get_model()[source]

Return the model the item belongs to

Returns:the model the item belongs to or None if it belongs to none
Return type:TreeModel | None
Raises:None
set_model(model)[source]

Set the model the item belongs to

A TreeItem can only belong to one model.

Parameters:model (Treemodel) – the model the item belongs to
Returns:None
Return type:None
Raises:None
add_child(child)[source]

Add child to children of this TreeItem

Parameters:child (TreeItem) – the child TreeItem
Returns:None
Return type:None
Raises:None
remove_child(child)[source]

Remove the child from this TreeItem

Parameters:child (TreeItem) – the child TreeItem
Returns:None
Return type:None
Raises:ValueError
child(row)[source]

Return the child at the specified row

Parameters:row (int) – the row number
Returns:the child
Return type:TreeItem
Raises:IndexError
child_count()[source]

Return the number of children

Returns:child coun
Return type:int
Raises:None
row()[source]

Return the index of this tree item in the parent rows

Returns:the row of this TreeItem in the parent
Return type:int
Raises:None
column_count()[source]

Return the number of columns that the children have

If there are no children, return the column count of its own data.

Returns:the column count of the children data
Return type:int
Raises:None
data(column, role)[source]

Return the data for the column and role

Parameters:
  • column (int) – the data column
  • role (QtCore.Qt.ItemDataRole) – the data role
Returns:

data depending on the role

Return type:

Raises:

None

set_data(column, value, role)[source]

Set the data of column to value

Parameters:
  • column (int) – the column to set
  • value – the value to set
  • role (QtCore.Qt.ItemDataRole) – the role, usually EditRole
Returns:

True, if data successfully changed

Return type:

bool

Raises:

None

parent()[source]

Return the parent tree item

Returns:the parent or None if there is no parent
Return type:TreeItem
Raises:None
set_parent(parent)[source]

Set the parent of the treeitem

Parameters:parent (TreeItem | None) – parent treeitem
Returns:None
Return type:None
Raises:None
itemdata()[source]

Return the internal ItemData

Returns:the internal ItemData
Return type:ItemData
Raises:None
internal_data()[source]

Return the internal data of the item data

E.g. a ListItemData could return the list it uses, a ProjectItemData could return the Project etc.

Returns:the data the itemdata uses as information
Return type:None|arbitrary data
Raises:None
flags(index)[source]

Return the flags for the item

Parameters:index (QtCore.QModelIndex) – the index to query
Returns:the flags
Return type:QtCore.Qt.ItemFlags
Raises:None
to_index(column=0)[source]

Return the index for this tree item in the model

Returns:The index in the model or None, if there is no model
Return type:QtCore.QModelIndex
Raises:None