5/30/2023 0 Comments Treeview modelThe child() and childCount() functions allow the model to obtain information about any child items.The appendChildItem() is used to add data when the model is first constructed and is not used during normal use.The functions provide the following features: It is used to hold a list of QVariants, containing column data, and information about its position in the tree structure. It does not inherit from QObject or provide signals and slots. The TreeItem class is defined as follows:Įxplicit TreeItem( const QList &data, TreeItem *parentItem = nullptr) With the appropriate data structure in place, we can create a tree model with a minimal amount of extra code to supply model indexes and data to other components. This makes writing the model easier and ensures that all model indexes that refer to the same item have the same internal data pointer. The use of a pointer-based tree structure means that, when passing a model index to a view, we can record the address of the corresponding item in the index (see QAbstractItemModel::createIndex()) and retrieve it later with QModelIndex::internalPointer(). For simplicity, we will use a list of QVariant objects to store the data for each column in the item. Since each item in a tree view usually contains several columns of data (a title and a summary in this example), it is natural to store this information in each item. Having this information readily available makes implementing the model easier. However, the root item in the tree structure has no parent item and it is never referenced outside the model.Įach TreeItem contains information about its place in the tree structure it can return its parent item and its row number. Generally, each TreeItem has a parent item, and can have a number of child items. The data is stored internally in the model using TreeItem objects that are linked together in a pointer-based tree structure. Each TreeItem represents an item in a tree view, and contains several columns of data. The data structure that we use to represent the structure of the data takes the form of a tree built from TreeItem objects. In this example, we will implement an internal structure to hold data rather than discuss how to package data from an external source. More generally, models can be used to represent data in the form of a tree structure by allowing each item to act as a parent to a table of child items.īefore attempting to implement a tree model, it is worth considering whether the data is supplied by an external source, or whether it is going to be maintained within the model itself. Simple models represent data as a table of items, and allow views to access this data via an index-based system. Qt's model/view architecture provides a standard way for views to manipulate information in a data source, using an abstract model of the data to simplify and standardize the way it is accessed.
0 Comments
Leave a Reply. |