tree_widget
Provides a custom QTreeView widget and a tree model to display hierarchical data.
Classes:
BRMSTreeWidget
BRMSTreeWidget(
columns: list[str], parent: QWidget | None = None
)
Bases: QTreeView
Methods:
-
clear_data
–Clear all data from the tree.
-
focusInEvent
–Detect when the TreeView gains focus.
-
populate_data
–Add data to the tree.
Attributes:
-
focused
–BRMSTreeWidget is a QTreeView that displays a tree structure with custom data.
focused
class-attribute
instance-attribute
focused = Signal()
BRMSTreeWidget is a QTreeView that displays a tree structure with custom data.
clear_data
clear_data() -> None
Clear all data from the tree.
focusInEvent
focusInEvent(event)
Detect when the TreeView gains focus.
populate_data
populate_data(
data: dict[str, str | object] | list[dict],
*,
clear_existing: bool = True,
expand: bool = True,
) -> None
Add data to the tree.
TreeItem
TreeItem(
data: list[TreeItemDataType],
parent: Optional[TreeItem] = None,
)
TreeItem represents a single item in a tree structure.
Methods:
-
append_child
–Append a child item to this item.
-
child
–Return the child item at the given row.
-
child_count
–Return the number of child items.
-
column_count
–Return the number of columns.
-
data
–Return the data for the given column.
-
parent
–Return the parent item.
-
row
–Return the row number of this item.
child_count
child_count() -> int
Return the number of child items.
column_count
column_count() -> int
Return the number of columns.
data
data(column: int) -> TreeItemDataType | None
Return the data for the given column.
row
row() -> int
Return the row number of this item.
TreeModel
TreeModel(
headers: list[TreeItemDataType],
parent: QWidget | None = None,
)
Bases: QAbstractItemModel
TreeModel provides a model for a tree structure to be used with QTreeView.
Methods:
-
add_data
–Add data to the tree model.
-
columnCount
–Return the number of columns.
-
data
–Return the data stored under the given role for the item referred to by the index.
-
find_data
–Find data in the tree model by searching for the given value in the specified column.
-
flags
–Return the item flags for the given index.
-
headerData
–Return the header data for the given section, orientation, and role.
-
index
–Return the index of the item in the model specified by the given row, column, and parent index.
-
parent
–Return the parent index of the given child index.
-
remove_data
–Remove data from the tree model based on id.
-
rowCount
–Return the number of rows.
-
update_data
–Update data in the tree model at the given index with new data.
add_data
add_data(parent: QModelIndex, data: list[dict]) -> None
Add data to the tree model.
data
is a list of dictionaries, where each dictionary represents a row with multiple columns.
Each dictionary can have a special key _children
to hold sub-items.
The key of each dict represents the column order.
For example, data
may be [{0: 'Property1', 1: 'Value1'}, {0: 'Property2', 1: 'Value2'}]
Then,
- Row 1: column 1 is 'Property1', column 2 is 'Value1'
- Row 2: column 1 is 'Property2', column 2 is 'Value2'
With children, data
may be
[{0: 'Property1', 1: 'Value1'}, {0: 'PropertyGroup', 1: '', '_children': [{0: 'Sub-property', 1: 'Sub-value'}]}]
columnCount
columnCount(parent: ModelIndex = QMODELINDEX) -> int
Return the number of columns.
data
data(
index: ModelIndex, role: int = DisplayRole
) -> Any | None
Return the data stored under the given role for the item referred to by the index.
find_data
find_data(
search_value: Any, column: int = 0
) -> QModelIndex | None
Find data in the tree model by searching for the given value in the specified column.
flags
flags(index: ModelIndex = QMODELINDEX) -> ItemFlag
Return the item flags for the given index.
headerData
headerData(
section: int,
orientation: Orientation,
role: int = DisplayRole,
) -> Any
Return the header data for the given section, orientation, and role.
index
index(
row: int, column: int, parent: ModelIndex = QMODELINDEX
) -> QModelIndex
Return the index of the item in the model specified by the given row, column, and parent index.
parent
parent(child: ModelIndex = QMODELINDEX) -> QModelIndex
Return the parent index of the given child index.
remove_data
remove_data(
parent: QModelIndex, id: UUID, id_column: int = 0
) -> None
Remove data from the tree model based on id.
rowCount
rowCount(parent: ModelIndex = QMODELINDEX) -> int
Return the number of rows.
update_data
update_data(
index: QModelIndex,
new_data: dict[int, TreeItemDataType],
) -> None
Update data in the tree model at the given index with new data.