Class ListModel<T>
Provides a strongly typed base class for implementing a flat list model.
public abstract class ListModel<T> : ListModel
Type Parameters
TThe item type exposed by the model. This can be a simple value such as string or a custom type with multiple public properties.
- Inheritance
-
ListModel<T>
- Inherited Members
Remarks
Derive from ListModel<T> when each row in the list can be represented as a
value of type T. Implement ItemCount() to report the
number of items and Data(int) to return the item at a given row.
If T is a simple convertible type such as string or
int, the model automatically exposes built-in display, edit,
and item roles. If T is a custom type, the model can derive
values from IDisplayable, IEditable, and
IModelItem, and it also exposes each public instance property as an
additional named role.
The generic list model follows the same item-type conventions as TableModel<T> while keeping a one-dimensional shape. If you need fully custom role names or custom role lookup logic, derive from ListModel instead and override RoleNames() and Data(ModelIndex, int) yourself.
public class NameList : ListModel<string>
{
private List<string> Names { get; } = ["Ada", "Linus", "Grace"];
public override int ItemCount() => Names.Count;
public override string Data(int index)
{
if (index < 0 || index >= Names.Count)
return null;
return Names[index];
}
}
Properties
HasItemRole
Gets whether the whole item should be exposed through the item role.
protected virtual bool HasItemRole { get; }
Property Value
Remarks
The default implementation returns true for backward compatibility with existing list-model usage patterns.
IsReadOnly
Gets whether the model should reject all edit operations.
protected virtual bool IsReadOnly { get; }
Property Value
Remarks
When this property returns true, the model does not expose the built-in edit role and SetData(ModelIndex, object, int) always returns false.
RoleMap
Gets the role-to-property map inferred from T.
protected Dictionary<int, PropertyInfo> RoleMap { get; }
Property Value
Remarks
For custom item types, each public instance property is assigned the next available role
id after the optional item role and exposed under its QML-style property name.
Methods
BeginInsertItems(int, int)
Begins an item insertion notification for the specified inclusive item range.
protected void BeginInsertItems(int first, int last)
Parameters
Remarks
Call this immediately before inserting items into the backing collection. Pair it with
EndInsertItems() in a try/finally block.
BeginRemoveItems(int, int)
Begins an item removal notification for the specified inclusive item range.
protected void BeginRemoveItems(int first, int last)
Parameters
Remarks
Call this immediately before removing items from the backing collection. Pair it with
EndRemoveItems() in a try/finally block.
ClearItemData(ModelIndex)
Clears the value associated with the specified row.
public override sealed bool ClearItemData(ModelIndex index)
Parameters
indexModelIndex
Returns
Remarks
This forwards a valid model index to ClearItemData(int) and rejects invalid indexes.
ClearItemData(int)
Clears the value associated with the specified row.
protected virtual bool ClearItemData(int index)
Parameters
indexint
Returns
Remarks
Override this when your list supports a dedicated clear operation distinct from setting a normal value. The default implementation returns false.
Data(ModelIndex, int)
Returns the value for the specified row and role.
public override sealed object Data(ModelIndex index, int role)
Parameters
indexModelIndexroleint
Returns
Remarks
For simple value types, the built-in display and edit roles return the item directly.
For custom item types, the base class uses IDisplayable,
IEditable, the optional item role, and public instance properties
to resolve role values.
Data(int)
Returns the item at the specified row.
public abstract T Data(int index)
Parameters
indexint
Returns
- T
Remarks
Implement this as a simple row lookup against your backing collection. The generic base
class validates the incoming Qt.DotNet.ModelIndex before calling this method. Return
null for invalid rows when T is a reference
type.
DataChanged(int)
Notifies that a single list item changed in place.
protected void DataChanged(int index)
Parameters
indexint
EndInsertItems()
Ends the current item insertion notification sequence.
protected void EndInsertItems()
EndRemoveItems()
Ends the current item removal notification sequence.
protected void EndRemoveItems()
Flags(ModelIndex)
Returns the capability flags for the specified item.
public override sealed int Flags(ModelIndex index)
Parameters
indexModelIndex
Returns
Remarks
The base implementation derives enabled/selectable state from IModelItem
when available and derives editability from IsReadOnly,
T, and IEditable.
ItemCount()
Returns the number of items in the list.
public abstract int ItemCount()
Returns
RoleNames()
Returns the role names inferred from T.
public override sealed Dictionary<int, string> RoleNames()
Returns
Remarks
Depending on the item type, this can include the built-in display and
edit roles, the optional item role for the whole list item, and
property-based roles generated from public instance properties.
RowCount(ModelIndex)
Returns the number of rows for the specified parent index.
public override sealed int RowCount(ModelIndex parent)
Parameters
parentModelIndex
Returns
Remarks
For the root level, this returns ItemCount(). For valid parent indexes, it
returns 0 because ListModel<T> is always a flat list.
SetData(ModelIndex, object, int)
Updates the value for the specified row and role.
public override sealed bool SetData(ModelIndex index, object value, int role)
Parameters
Returns
Remarks
For editable simple value types, this updates the item through SetData(int, T).
For custom item types, it updates either EditValue, the full
item via the item role, or a writable public property. Successful updates
automatically notify connected views.
SetData(int, T)
Updates the item at the specified row.
protected virtual bool SetData(int index, T value)
Parameters
indexintvalueT
Returns
Remarks
Override this when the list should support replacing whole items, such as for editable
simple value types or when exposing the item role for custom types.