12579 views|1 replies

661

Posts

18

Resources
The OP
 

Qt Learning Road 45 Model [Copy link]

Based on the previous two chapters, we will begin to introduce the general concepts of models. In the model/view architecture, the model provides a standard interface for views and delegates to access data. In Qt, this interface is defined by the QAbstractItemModel class. Regardless of how the underlying data is stored, any subclass of QAbstractItemModel provides a tabular hierarchy. Views use uniform conversions to access the data in the model. However, it should be noted that although the model organizes data internally in this way, it is not required to display the data to the user in this way. , sans-serif]

, 247, 247)] Item (root node), and below it are linear data. These data can actually be regarded as a table with only one column, but it is hierarchical because there is a root node. Table Model is easier to understand, but it also has a root node. Tree Model is mainly oriented to hierarchical data, and each level can have many columns, so it is also a hierarchical table.

In order to separate the display of data from the storage, we introduce the concept of model index. Through the index, we can access specific parts of specific elements of the model. Views and delegates use indexes to request the required data. From this, we can see that only the model itself needs to know how to get data, and the data types managed by the model can be defined in a generic way. The index holds a pointer to the model that created it, which makes it possible to operate multiple models at the same time.

  • QAbstractItemModel *model = index.model();
[color=rgb(0, 204, 255) !important]Copy code

Model indexes provide temporary indexes of the required information and can be used to retrieve or modify data through the model. Since the model may reorganize its internal structure at any time, the model index is likely to become unavailable, and at this time, the data should not be saved. If you need a long-term valid piece of data, you must create a persistent index. A persistent index ensures that the data it references is updated in a timely manner. Temporary indexes (that is, the indexes commonly used) are provided by the QModelIndex class, and persistent indexes are provided by the QPersistentModelIndex class.

In order to locate the data in the model, we need three attributes: row number, column number and parent index. Let's explain them one by one.

We have introduced the basic form of the model before: data is stored in the form of a two-dimensional table. At this time, a piece of data can be located by row number and column number. Note that we only use the term "two-dimensional table", which does not mean that the model is really stored in the form of a two-dimensional array; the so-called "row number" and "column number" are just for the convenience of describing this correspondence, and there is no real distinction between rows and columns. By specifying the row number and column number, we can locate an element item and retrieve its information. 5, 204, 255) !important]复制代码

[backcolor=rgb(247, 247, 204, 255) !important]复制代码

The model provides a simple interface for data retrieval in non-hierarchical views such as lists and tables. However, as the above code suggests, the actual interface is not that simple. We can view the prototype of this function through the document:

  • QModelIndex QAbstractItemModel::index(int row,
  • int column,
  • const QModelIndex &parent=QModelIndex()) const
[color=rgb(0, 204, 255) !important]Copy code

Here, we only use the first two parameters. You can understand it through the following picture:

, QModelIndex()); [color=rgb(0, 204, 255) !important]复制代码 [p=22, null, , sans-serif]The last parameter of the function is always QModelIndex(). Next we will discuss the meaning of this parameter.

Helvetica, sans-serif]In table-like views, such as lists and tables, row and column numbers are sufficient to locate a data item. However, for tree structures, only two parameters are not enough. This is because the tree structure is a hierarchical structure, and each node in the hierarchy may be another table. Therefore, each item needs to specify its parent node. 55555]

  • QModelIndex index = model->index(row, column, parent);
[color=rgb(0, 204, 255) !important]复制代码

Similarly, let's look at the following diagram:

, 247)]In the figure, A and C are both top-level items in the model:

  • QModelIndex indexA = model->index(0, 0, QModelIndex());
  • QModelIndex indexC = model->index(2, 1, QModelIndex());
[color=rgb(0, 204, 255) !important]复制代码

A also has its own sub-items. 55555]

  • QModelIndex indexB = model->index(1, 0, indexA);
[color=rgb(0, 204, 255) !important]复制代码

[backcolor=rgb(247, 247, , Helvetica, SimSun, sans-serif]We have discussed the location of indexes. Now let's look at another part of the model: data roles. Models can provide different data for different components (or different parts of components, such as the tooltip of a button and the displayed text). For example, Qt::DisplayRole is used for text display in views. Generally speaking, data items contain a series of different data roles, which are defined in the Qt::ItemDataRole enumeration. We can get the data provided by the model by specifying the index and role:

By providing appropriate data for each role, the model can tell the view and delegate how to display content to the user. Different types of views can choose to ignore the data they don't need. Of course, we can also add additional data as needed. :

To summarize:

  • The model uses indexes to provide views and delegates with information about the location of data items. The advantage of this is that objects outside the model do not need to know how the underlying data is stored;
  • Data items are located by three coordinates: row number, column number, and parent item;
  • Model indexes are created by the model when other components (views and delegates) request them;
  • If you use index() The function requests an available index of a parent item, which points to the data item under the parent item in the model. This index points to a child of the item; if the index() function is used to request an unavailable index of a parent item, the index points to the top-level item of the model;
  • The role is used to distinguish different types of data in the data item.

Let's go back to the QFileSystemModel model we have seen before and see how to get data from the model.

  • QFileSystemModel *model = new QFileSystemModel;
  • QModelIndex parentIndex = model->index(QDir::currentPath());
  • int numRows = model->rowCount(parentIndex);
[color=rgb(0, 204, 255) !important]Copy code

In this example, we create an instance of QFileSystemModel, use the overloaded index() of QFileSystemModel to get the index, and then use the rowCount() function to calculate how many data items (that is, the number of rows) there are in the current directory. The confusing code in the previous chapter is now quite clear.

For simplicity, we only care about the first column of the model below. We iterate over all the data and get the first column index:

  • for (int row = 0; row < numRows; ++row) {
  • QModelIndex index = model->index(row, 0, parentIndex);
[color=rgb(0, 204, 255) !important]Copy code

We use the index() function. The first parameter is the row number of each row, the second parameter is 0, which is the first column, and the third parameter is parentIndex, which is the current directory as the parent. We can use the model's data() function to get the data of each item. Note that the return value of this function is QVariant, which is actually a string, so we convert it directly into QString:

  • QString text = model->data(index, Qt::DisplayRole).toString();
  • // Use text data
  • }
[color=rgb(0, 204, 255) !important]Copy code

The above code snippet shows some useful functions for getting data from the model:

  • The number of models can be obtained through rowCount() and columnCount(). These functions require the parent item to be specified;
  • Index is used to access data in the model. We need to use three parameters: row number, column number and parent item to obtain the index;
  • When we use QModelIndex() to create an empty index, we get the top-level item in the model;
  • Data items contain data for different roles. To obtain data for a specific role, you must specify the role.

This post is from Embedded System

Latest reply

  Details Published on 2018-4-28 11:26

935

Posts

1

Resources
2
 
This post is from Embedded System
 
Personal signature存储芯片/MCU/SRAM/PSRAM/DDR/FLASH/MRAM。web.www.sramsun.com  QQ3161422826 TEL:13751192923
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list