Class NodeTreeModel<T>
Provides a strongly typed base class for adapting an existing single-column tree.
public abstract class NodeTreeModel<T> : TreeModel where T : class
Type Parameters
TThe reference type used for tree nodes.
- Inheritance
-
NodeTreeModel<T>
- Derived
- Inherited Members
Remarks
Derive from NodeTreeModel<T> when each item in a hierarchy is represented by a node object. Implement the root and child lookup members to describe the tree, and implement ParentOf(T) and IndexOf(T) so the model can navigate from an item back to its parent.
The base class assigns each node a stable internal model id using reference identity. A node must therefore occur at most once in the tree and must remain the same object while it is exposed by the model. Report structural changes with the protected begin/end helpers before and after changing the backing tree, so connected views can update correctly.
T is constrained to a reference type because a tree index must
identify one stable node object, and null represents the absence of a
parent for root nodes. Value types do not provide either convention reliably when copied or
boxed.
Single-column means that each node provides one displayed item or value; it does not limit the depth of the hierarchy. Use a multi-column tree when each node needs sibling values, such as a file name, size, and modified date.
Public instance properties on T are exposed automatically as
named values, called roles, that are available to QML. IDisplayable,
IEditable, and
IModelItem provide the same display, editing, and flag conventions as
ListModel<T> and TableModel<T>.
important
This model exposes exactly one column for each node. For a hierarchical model with multiple columns, derive from the non-generic TreeModel and implement its index, parent, column, and value lookup behavior for the application's data structure.
public sealed class FolderNode(string name)
{
public string Name { get; } = name;
public FolderNode Parent { get; private set; }
public List<FolderNode> Children { get; } = [];
public FolderNode Add(FolderNode child)
{
child.Parent = this;
Children.Add(child);
return this;
}
}
public sealed class FolderTree : NodeTreeModel<FolderNode>
{
private readonly List<FolderNode> roots =
[
new FolderNode("Projects")
.Add(new FolderNode("Qt"))
.Add(new FolderNode("CSharp"))
];
protected override int RootCount => roots.Count;
protected override FolderNode RootAt(int row) => roots[row];
protected override int ChildCount(FolderNode parent) => parent.Children.Count;
protected override FolderNode ChildAt(FolderNode parent, int row) =>
parent.Children[row];
protected override FolderNode ParentOf(FolderNode node) => node.Parent;
protected override int IndexOf(FolderNode node) =>
node.Parent is null ? roots.IndexOf(node) : node.Parent.Children.IndexOf(node);
}
Properties
HasItemRole
Gets whether the complete node is exposed through the item role.
protected virtual bool HasItemRole { get; }
Property Value
IsReadOnly
Gets whether the model should reject all edit operations.
protected virtual bool IsReadOnly { get; }
Property Value
RoleProperties
Gets the public node properties exposed as automatic QML roles.
protected virtual IEnumerable<PropertyInfo> RoleProperties { get; }
Property Value
RootCount
Gets the number of root-level nodes.
protected abstract int RootCount { get; }
Property Value
Methods
ChildAt(T, int)
Returns the child of parent at row.
protected abstract T ChildAt(T parent, int row)
Parameters
parentTrowint
Returns
- T
ChildCount(T)
Returns the number of children of parent.
protected abstract int ChildCount(T parent)
Parameters
parentT
Returns
ClearItemData(ModelIndex)
Clears all values associated with the specified item.
public override sealed bool ClearItemData(ModelIndex index)
Parameters
indexModelIndex
Returns
ClearNodeData(T)
Clears the value associated with node.
protected virtual bool ClearNodeData(T node)
Parameters
nodeT
Returns
ColumnCount(ModelIndex)
Returns the number of columns for the specified parent index.
public override sealed int ColumnCount(ModelIndex parent)
Parameters
parentModelIndex
Returns
Data(ModelIndex, int)
Returns the value stored for the specified item and role.
public override sealed object Data(ModelIndex index, int role)
Parameters
indexModelIndexroleint
Returns
DataChanged(T)
Notifies that the data for node changed.
protected void DataChanged(T node)
Parameters
nodeT
Flags(ModelIndex)
Returns the capability flags for the specified item.
public override sealed int Flags(ModelIndex index)
Parameters
indexModelIndex
Returns
Remarks
The default implementation marks items as enabled and selectable.
HasChildren(ModelIndex)
Returns whether the specified parent item has any children.
public override sealed bool HasChildren(ModelIndex parent)
Parameters
parentModelIndex
Returns
Index(int, int, ModelIndex)
Creates an index that identifies the item at the specified row and column under a parent.
public override sealed ModelIndex Index(int row, int column, ModelIndex parent)
Parameters
Returns
- ModelIndex
IndexOf(T)
Returns the current row of node under its parent.
protected abstract int IndexOf(T node)
Parameters
nodeT
Returns
Parent(ModelIndex)
Returns the parent index for the specified item.
public override sealed ModelIndex Parent(ModelIndex index)
Parameters
indexModelIndex
Returns
- ModelIndex
ParentOf(T)
Returns the parent of node, or null for a root
node.
protected abstract T ParentOf(T node)
Parameters
nodeT
Returns
- T
RoleNames()
Returns the names used to address values exposed by this model.
public override sealed Dictionary<int, string> RoleNames()
Returns
Remarks
A role identifies a value that can be requested from an item, such as a display string, an editable value, or a custom property. Return a dictionary whose keys are role ids, such as DisplayRole or values starting at UserRole, and whose values are stable, consumer-facing role names.
RootAt(int)
Returns the root-level node at row.
protected abstract T RootAt(int row)
Parameters
rowint
Returns
- T
RowCount(ModelIndex)
Returns the number of child rows under the specified parent index.
public override sealed int RowCount(ModelIndex parent)
Parameters
parentModelIndex
Returns
SetData(ModelIndex, object, int)
Updates the value stored for the specified item and role.
public override sealed bool SetData(ModelIndex index, object value, int role)
Parameters
Returns
Remarks
Call DataChanged(ModelIndex, ModelIndex, int[]) after a successful in-place edit so that connected consumers refresh the affected data.
SetNode(T, int, T)
Replaces the node at the specified row under parent.
protected virtual bool SetNode(T parent, int row, T value)
Parameters
parentTrowintvalueT
Returns
Sibling(int, int, ModelIndex)
Returns another item with the same parent as the specified item.
public override sealed ModelIndex Sibling(int row, int column, ModelIndex index)
Parameters
Returns
- ModelIndex
TryCreateIndex(T, out ModelIndex)
Creates an index for an existing node.
protected bool TryCreateIndex(T node, out ModelIndex index)
Parameters
nodeTindexModelIndex