

As a result, each TreeItem maintains information about its parent and children, making it possible for us to traverse the tree structure. Similarly, a function like TreeItem::child() is helpful when implementing the model's index() function. When designing a data structure for use with a custom model, it is useful to expose each item's parent via a function like TreeItem::parent() because it will make writing the model's own parent() function easier. Although this makes the tree model less flexible, and possibly less useful for use with more sophisticated views, it makes it less complex to design and easier to implement. Since QTreeView provides a row-oriented view onto a model, it is natural to choose a row-oriented design for data structures that will supply data via a model to this kind of view. Each TreeItem is designed to hold data for a row of items in a tree view, so it contains a list of values corresponding to the data shown in each column.

DesignĪs with the Simple Tree Model example, the model simply acts as a wrapper around a collection of instances of a TreeItem class. Since this example allows the dimensions of the model to be changed, we must also implement insertRows(), insertColumns(), removeRows(), and removeColumns(). In addition, hierarchical models, such as this one, need to provide implementations of index() and parent().Īn editable model needs to provide implementations of setData() and setHeaderData(), and must return a suitable combination of flags from its flags() function. OverviewĪs described in the Model Subclassing Reference, models must provide implementations for the standard set of model functions: flags(), data(), headerData(), columnCount(), and rowCount(). With these features, it is also possible to insert new child items, and this is shown in the supporting example code. The model supports editable items, custom headers, and the ability to insert and remove rows and columns.
