model/view 結構

model/view 結構

概述

The model communicates with a source of data, providing an interface for the other components in the architecture. The nature of the communication depends on the type of data source, and the way the model is implemented.The view obtains model indexes from the model; these are references to items of data. By supplying model indexes to the model, the view can retrieve items of data from the data source. In standard views, a delegate renders the items of data. When an item is edited, the delegate communicates with the model directly using model indexes.

model 用來和數據通信,爲View,delegate 提供接口。 model和數據通信的方式 和數據源的類型以及model的實現方式有關。view 從 model 獲取 model index。這些index 是 數據項的引用。 通過把model indexes提供給model, view可以從數據源中獲取數據 。在標準的views中,delegte 對數據項進行渲染。當某個數據項被選中,delgate通過model index 直接與model交流。

Generally, the model/view classes can be separated into the three groups described above: models, views, and delegates. Each of these components is defined by abstract classes that provide common interfaces and, in some cases, default implementations of features. Abstract classes are meant to be subclassed in order to provide the full set of functionality expected by other components; this also allows specialized components to be written.

這個結構分爲三個部分,models,views,delegates. 每個部分都被定義爲抽象類,用來提供高一些通用的接口。在一些情況下,有默認實現方式。
三者的信息傳遞 通過 槽機制(signal/slots)

Models, views, and delegates communicate with each other using signals and slots:
1. Signals from the model inform the view about changes to the data held by the data source.
2. Signals from the view provide information about the user’s interaction with the items being displayed.
3. Signals from the delegate are used during editing to tell the model and view about the state of the editor.

來自model 的信號告知view 數據源數據的變化
來自 view 的信號用戶和展示出的項目的交互信息
來自delegate 的信號被用來通知model,view editor的狀態(在被選中的時候)

Models

基類: QAbstractItemModel。 所有的類都以此爲基礎。 該類定了一個接口用於views和delegates 與data的通道。
另外:QAbstractListModel,QAbstractTableModel爲 list,Table 提供各類很好的方法。還有一些設定好的類:
QStringListModel,QStandardItemModel,QFileSystemModel,QSqlQueryModel,QSqlTableModel,QSqlReleationTableModel. 也可以繼承前面的三個基類,來自定義model.

Qt提供了兩個標準的models:QStandardItemModel和QDirModel。QStandardItemModel是一個多用途的model,可用於表示list,table,tree views所需要的各種不同的數據結構。這個model也持有數據。QDirModel維護相關的目錄內容的信息,它本身不持有數據,僅是對本地文件系統中的文件與目錄的描述。QDirModel是一個現成的model,很容易進行配置以用於現存的數據,使用這個model,可以很好地展示如何給一個現成的view設定model,研究如何用model indexes來操縱數據。

Views

基類: QAbstractItemView.
另外Complete implementations are provided for different kinds of views:
QListView displays a list of items,
QTableView displays data from a model in a table,
QTreeView shows model items of data in a hierarchical list.

Delegates

基類:QAbstractItemDelegate
默認的delegate 接口是QStyleItemDelegate。他和 QItemDelegate 是相互獨立可選擇的。
兩者區別在於: QStyledItemDelegate uses the current style to paint its items。 文檔上說 推薦使用QStyledItemDelegate

例子

    QSplitter *splitter = new QSplitter;
    //初始化一個已經存在model
    QFileSystemModel *model = new QFileSystemModel;
    model->setRootPath(QDir::currentPath());

    //初始化一個 treeView
    QTreeView  *tree = new QTreeView(splitter);
    //讓tree在model中展示
    tree->setModel(model);
    //設置model index
    tree->setRootIndex(model->index(QDir::currentPath()));
    //index() 用來返回一個model index

    QListView *list = new QListView(splitter);
    list->setModel(model);
    tree->setRootIndex(model->index(QDir::currentPath()));

    splitter->setWindowTitle("Differences");
    splitter->show();

model index

作用: 保證數據表示 和獲得主句的通道 分離。通過model index,可以引用model中的數據項,Views和delegates都使用indexes來訪問數據項,然後再顯示出來。
因此,只有model需要了解如何獲取數據,被model管理的數據類型可以非常廣泛地被定義。Model indexes包含一個指向創建它們的model的指針,這會在配合多個model工作時避免混亂。

QAbstractItemModel *model = index.model();

model indexes提供了對一項數據信息的臨時引用,通過它可以訪問或是修改model中的數據。既然model有時會重新組織內部的數據結構,這時model indexes便會失效,因此不應該保存臨時的model indexes。假如需要一個對數據信息的長期的引用,那麼應該創建一個persistent model index。這個引用會保持更新。臨時的model indexes由QModelIndex提供,而具有持久能力的model indexes則由QPersistentModelIndex提供。在獲取對應一個數據項的model index時,需要考慮有關於model的三個屬性:行數,列數,父項的model index。

    QFileSystemModel *model = new QFileSystemModel;
    QModelIndex parentIndex = model->index(QDir::currentPath());
    int numRows = model->rowCount();
    qDebug()<<"munRows"<<numRows;

    for (int row = 0; row < numRows ; row ++){
        QModelIndex index = model->index(row,0,parentIndex);
        QString text = model->data(index,Qt::DisplayRole).toString();
        qDebug()<<text;
        }
  • the dimensions of a model can be found using rowCount() and columnCount(). These functions generally require a parent model index to be specified.
  • Model indexes are used to access items in the model. The row, column, and parent model index are needed to specify the item.
  • To access top-level items in a model, specify a null model index as the parent index with QModelIndex().
  • Items contain data for different roles. To obtain the data for a particular role, both the model index and the role must be supplied to the model.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章