Class TreeModel<T>
Provides a single-column tree model backed by TreeNode<T> nodes.
public class TreeModel<T> : NodeTreeModel<T> where T : TreeNode<T>
Type Parameters
TThe concrete node type.
- Inheritance
-
TreeModel<T>
- Inherited Members
Remarks
Create this model directly when application nodes can inherit TreeNode<T>. The parameterless constructor creates an empty root collection. Alternatively, pass an IList<T> to the constructor to use an existing root collection. Navigation and identity handling are supplied by the base class.
After the model is used by a view, change its structure only through AddRoot(T), InsertRoot(int, T), AddChild(T, T), InsertChild(T, int, T), and Remove(T). These methods notify connected views about the change.
Public properties declared by T become named values available to
QML automatically. The inherited Parent and
Children properties describe navigation state, so they are not
exposed as QML values.
Single-column means that each node provides one displayed item or value; it does not limit the depth of the hierarchy. Use the non-generic TreeModel when each node needs sibling values, such as a file name, size, and modified date.
important
This convenience model exposes exactly one column for each node. Use the non-generic TreeModel when each node needs multiple columns.
public sealed class FolderNode(string name) : TreeNode<FolderNode>
{
public string Name { get; } = name;
}
var folders = new TreeModel<FolderNode>();
var projects = folders.AddRoot(new FolderNode("Projects"));
folders.AddChild(projects, new FolderNode("Qt"));
Constructors
TreeModel()
Initializes a model with an empty root collection.
public TreeModel()
TreeModel(IList<T>)
Provides a single-column tree model backed by TreeNode<T> nodes.
public TreeModel(IList<T> rootNodes)
Parameters
rootNodesIList<T>
Remarks
Create this model directly when application nodes can inherit TreeNode<T>. The parameterless constructor creates an empty root collection. Alternatively, pass an IList<T> to the constructor to use an existing root collection. Navigation and identity handling are supplied by the base class.
After the model is used by a view, change its structure only through AddRoot(T), InsertRoot(int, T), AddChild(T, T), InsertChild(T, int, T), and Remove(T). These methods notify connected views about the change.
Public properties declared by T become named values available to
QML automatically. The inherited Parent and
Children properties describe navigation state, so they are not
exposed as QML values.
Single-column means that each node provides one displayed item or value; it does not limit the depth of the hierarchy. Use the non-generic TreeModel when each node needs sibling values, such as a file name, size, and modified date.
important
This convenience model exposes exactly one column for each node. Use the non-generic TreeModel when each node needs multiple columns.
public sealed class FolderNode(string name) : TreeNode<FolderNode>
{
public string Name { get; } = name;
}
var folders = new TreeModel<FolderNode>();
var projects = folders.AddRoot(new FolderNode("Projects"));
folders.AddChild(projects, new FolderNode("Qt"));
Properties
RoleProperties
Gets the public node properties exposed as automatic QML roles.
protected override IEnumerable<PropertyInfo> RoleProperties { get; }
Property Value
RootCount
Gets the number of root-level nodes.
protected override sealed int RootCount { get; }
Property Value
RootNodes
Gets a live, read-only view of the current root nodes.
public IReadOnlyList<T> RootNodes { get; }
Property Value
Methods
AddChild(T, T)
Appends a child node and notifies connected views.
public T AddChild(T parent, T child)
Parameters
parentTchildT
Returns
- T
AddRoot(T)
Appends a root node and notifies connected views.
public T AddRoot(T node)
Parameters
nodeT
Returns
- T
ChildAt(T, int)
Returns the child of parent at row.
protected override sealed T ChildAt(T parent, int row)
Parameters
parentTrowint
Returns
- T
ChildCount(T)
Returns the number of children of parent.
protected override sealed int ChildCount(T parent)
Parameters
parentT
Returns
IndexOf(T)
Returns the current row of node under its parent.
protected override sealed int IndexOf(T node)
Parameters
nodeT
Returns
InsertChild(T, int, T)
Inserts a child node at the specified row and notifies connected views.
public T InsertChild(T parent, int row, T child)
Parameters
parentTrowintchildT
Returns
- T
InsertRoot(int, T)
Inserts a root node at the specified row and notifies connected views.
public T InsertRoot(int row, T node)
Parameters
rowintnodeT
Returns
- T
ParentOf(T)
Returns the parent of node, or null for a root
node.
protected override sealed T ParentOf(T node)
Parameters
nodeT
Returns
- T
Remove(T)
Removes a node and its descendants from the model.
public bool Remove(T node)
Parameters
nodeT
Returns
RootAt(int)
Returns the root-level node at row.
protected override sealed T RootAt(int row)
Parameters
rowint
Returns
- T