QT concept of the model-view-delegate --- of QStandardItemModel 1

Class QabstractItemModel, QabstractListModel, QAbstractTableModel data is not saved, the user needs to subclass these classes and defines certain data structures to store data subclass.

In contrast, the class QStandardItemModel responsible for saving data, each data item is represented as an object QStandardItem class. Next, we introduce mainly from two aspects of the content of the class QStandardItemMode.

  1. First class QStandardItem save explains how to use a data item,
  2. Then explains how to use the class QStandardItemModel to organize these data items, the list is formed, or the tree table, view class for the other display.

First look at the first aspect:

As mentioned before, a data item consists of several "roles, data child" pairs. Class QStandardItem responsible for preservation, access to these data. Internal class defines a type QVector container,

Essentially each storage element of a container "character data child" right. Because each character data corresponding to the sub-items may have different types, Qt QVariant used to store each data sub-item.

When the user wants to store some data in a QStandardItem object, you can call its member functions:

void setData (const QVariant & value, int role) // The "role, value" of the deposit.

When the user wants to read the data object can call another member function:

QVariant data (int role =) const // read the character data corresponding to child role.

These two functions is the core of QStandardItem. With these two functions, we can access the class represented by any one data item "roles, data child" right. However, for some commonly used characters,

This class provides a more concise, easy to remember the member functions. For example, when a data item is displayed when the view, which often contains some text, an icon, may also contain a check box.

Common roles:

  • Qt :: BackgroundRole control the display background,
  • Qt :: FontRole control text font,
  • Qt :: ForegroundRole control text color,
  • Qt :: CheckStateRole control state of the checkbox.

This class provides a set of member functions can easily access these commonly used data sub-items corresponding to the role:

  • Member function setBackground (), background () are provided / back background brush.
  • Function setFont (), font () are set / return text font.
  • Function setForeground (), foreground () are provided / back font color.
  • Function setCheckState (), checkState () are provided / back checkboxes.

Then look at the second aspect:

The class QStandardItemModel class organization QStandardItem data items represented to form lists, tables, trees and even more complex data structures.

  • This class provides a set of member functions, add new items to these data structures, data entry has been change, or delete existing data items.
  • On the other hand, as a model class that implements the interface definition QAbstractItemModel function, so that other classes can access the data item view model.

1: If the data set is represented as a list, we can call the class member functions appendRow QStandardItemModel of () to add a data item to the list, use the item () reads a data item.

As shown in the code segment 13-10.

  • ① acquisition model topmost row of the root node,
  • Line ② create a QStandardItem object that represents a data item,
  • ③ The line items added to the list as a child node of the root node.

② row of the class constructor call setData inside () function, the line ② of Qt :: QString object as an object into a newly constructed DisplayRole corresponding data sub-items.

Since the data set is itself a list, so we use QListView display this data set, the reader can run this example View the results.

Snippet 13-10: Use QStandardItemModel processing list

Copy the code

    QStandardItemModel listModel;
    QStandardItem *rootItem = listModel.invisibleRootItem();    //    行1
    for (int row = 0; row < 4; ++row){

        QStandardItem *item = new QStandardItem(QString("%1").arg(row) );    //    行2
        rootItem->appendRow( item );    //行3
    }
    QListView listView;
    listView.setModel ( & listModel );

Copy the code

2: If the data set is represented as a table, you can call a member function of setItem QStandardItemModel class () to set a data item in the table, such as the code segment 13-11.

Since in this data set is a code segment table, the use of the data set QTableView display.

Snippet 13-11: Use QStandardItemModel processing forms

Copy the code

    QStandardItemModel tableModel(4, 4);
    for (int row = 0; row < 4; ++row){
        for (int column = 0; column < 4; ++column) {
            QStandardItem *item = new QStandardItem(QString("%0,%1").arg(row).arg(column));
            tableModel.setItem(row, column, item);
       }
    }
    QTableView tableView;
    tableView.setModel( & tableModel );

Copy the code

 

3: If the data set is represented as a tree, you can call the class member functions appendRow QStandardItemModel of () to add a child node to a tree node.

By repeatedly calling the function, you can build a complex tree. Code segments constructing a simple tree 13-12: there is a top-level root text is "0" child node,

The child node has a text to "1" child node. And so on, "1" child node has a "2" child nodes "2" has a child node "3" child node, forming a tree depth of 4.

Each node in the tree no sibling node (a plurality of nodes having the same parent are called siblings to each other), the interested reader can modify the code, so that some have a sibling node.

Since the data set is a tree, we use QTreeView display it.

Code segments 13-12, using the processing QStandardItemModel tree:

Copy the code

QStandardItemModel treeModel;
QStandardItem *parentItem = treeModel.invisibleRootItem();
for (int i = 0; i < 4; ++i) {
  QStandardItem *item = new QStandardItem(QString("%0").arg(i));
  parentItem->appendRow(item);
  parentItem = item;
}
QTreeView treeView;
treeView.setModel( & treeModel );

Copy the code

I was able to represent the class QStandardItemModel lists, tables, trees and even more complex data structures, thanks to its internal QStandardItem class defines a type of

QVector <QStandardItem *> container, QStandardItem each container element of the object referred to may be set as a child object. Performance in the class diagram shown in FIG. 13-13, and itself has QStandardItem class "children" relationship. And self association occurs a class, are called self correlation (self association) in UML. QStandardItemModel member defines a class called root data logically QStandardItem a pointer to the object. This object can be set to target multiple QStandardItem as its sub-objects, each of which can contain other child objects and sub-objects. And so on, the tree may have any depth, each parent object can contain any number of sub-objects.

 

 

Naturally, QStandardItemModel be used QStandardItem represent a tree data structure in a data set, 13-14.

图中的每个小方框表示类QStandardItem的一个对象。如果小方框的边线为虚,相应的QStandardItem对象并不表示数据集中的任何数据,仅被用来表示某种数据结构。如果小方框的边线为实,相应的QStandardItem对象就表示数据集中的一个数据项。在右侧的图中,QStandardItemModel的数据成员root所指的对象表示一个不可见的根,而数据集的根(图中结点G)被表示为这个不可见根的一个子节点。

 

 

列表被看作一个特殊的树:不可见根具有若干个子节点,每个子节点表示列表中的一个数据项,不再包含任何子节点,如该图左侧所示。

而表格的表示方式反而麻烦一些。不可见根含有若干子节点(图中A,B,C),这些子节点并不表示数据集中的任何数据项。

第i个子节点会包含若干子节点(比如图中D,E,F),这些子节点才表示表格第i行的数据项。

最后再讨论一下QStandardItemModel表示数据集的优缺点:

使用QStandardItemModel表示数据集具有以下优点:

  1. 该类使用QStandardItem存放数据项,用户不必定义任何数据结构来存放数据项;
  2. QStandardItem使用自关联关系,能够表达列表、表格、树甚至更复杂的数据结构,能够涵盖各种各样的数据集;
  3. QStandardItem本身存放着多个『角色,数据子项』,视图类、委托类或者其他用户定义的类能够方便地依据角色访问各个数据子项。

然而,这种表示方法也有局限性:

  • 当数据集中的数据项很多时,施加在数据集上的某些操作的执行效率会很低。

      比如,设数据集是一个1万行、20列的表格,其中第10列存放的是浮点数。如果我们想计算这一列的平均值,按照图13-14,这需要遍历所有行,

取得第10列的QStandardItem对象,再依据角色“Qt::DisplayRole”取得对应的数据子项。由于这个数据子项的类型为QString,还需要将其转换为浮点数,

最后求所有浮点数的平均值。这些操作会耗费较长的时间。

Therefore, the amount of data is not very big, not very high performance requirements of the occasion, we can use the class QStandardItemModel to represent a set of data.

Otherwise, the user should derive a new class from QAbstractItemModel, QAbstractListModel or QAbstractTableModel, self-managing storage and access to data sets

Published 34 original articles · won praise 4 · views 20000 +

Guess you like

Origin blog.csdn.net/u014725884/article/details/104269399