1 Introduction to Model/View Programming
<u>The model/view architecture</u>
一般的model/view classes可以分成三部分:models, views, and delegates 赃额,每一部分都有抽象基類提供通用接口手幢,這意味著你可以子類化他們來實現(xiàn)契約中的功能豺总,也可以實現(xiàn)一些你所定制的功能禽拔。
Models, views, and delegates使用信號槽機制通信:
- Signals from the model inform the view about changes to the data held by the data source.
- Signals from the view provide information about the user's interaction with the items being displayed.
- Signals from the delegate are used during editing to tell the model and view about the state of the editor.
Models
QAbstractItemModel是所有model的基類,它提供了足夠多的臼婆,靈活度高的接口來使view顯示數(shù)據(jù)惶洲。
- QStringListModel is used to store a simple list of QString items.
- QStandardItemModel manages more complex tree structures of items, each of which can contain arbitrary data.
- QFileSystemModel provides information about files and directories in the local filing system.
- QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel are used to access databases using model/view conventions.
Views
QAbstractItemView是三種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
默認使用:QStyledItemDelegate
Sorting
0...0
Convenience classes
- QListWidget, QTreeWidget, and QTableWidget 這些類方便使用,但不是為了子類化而設(shè)計的燃少,所以他們沒有views類靈活性高,不可以使用model铃在,還有重要的是他們是 item-based
- 如果你想在model/view的基礎(chǔ)上繼續(xù)使用item-based模型阵具,可以使用下面類: QListView, QTableView, and QTreeView with QStandardItemModel
2 Using models and views
下面介紹怎樣使用model/view模式
<u>Two models included in Qt</u>
- QStandardItemModel
多用途model,可以提供list定铜、table阳液、tree的數(shù)據(jù)結(jié)構(gòu),這個類中可以存儲數(shù)據(jù)宿稀。 - QFileSystemModel
提供文件目錄的信息趁舀,不存儲數(shù)據(jù)。
3 Model classes
<u>Basic concepts</u>
在model/view架構(gòu)中祝沸,model提供標準接口以便于 views and delegates可以訪問數(shù)據(jù)矮烹。在QT中,標準接口由QAbstractItemModel規(guī)定罩锐,無論數(shù)據(jù)項是怎么樣保存的奉狈,所有QAbstractItemModel的子類都是以一個層次結(jié)構(gòu)來表示數(shù)據(jù)。
Model內(nèi)部改變時涩惑,會通過信號槽機制來通知任何使用它的views
Model indexes
為了確保數(shù)據(jù)表示和數(shù)據(jù)的訪問無關(guān)仁期,引入了model index的概念。model中的每一個數(shù)據(jù)都可以由一個model index來表示竭恬, Views and delegates通過這些index獲取顯示數(shù)據(jù)項跛蛋。
這樣的結(jié)果就是只有model知道怎樣獲取數(shù)據(jù),通過模型管理數(shù)據(jù)類型痊硕。索引包含一個指向創(chuàng)建它們的模型的指針赊级,用多個模型時,可以防止混亂岔绸。
QAbstractItemModel *model = index.model();
模型索引提供了不同信息塊的一個臨時引用理逊,可用于檢索或修改數(shù)據(jù)。模型有時會重組其內(nèi)部結(jié)構(gòu)盒揉,索引會失效晋被,所以不要保存模型索引。如果需要一個長期有效的數(shù)據(jù)引用刚盈,一定要創(chuàng)建一個永久模型索引羡洛。
臨時索引: QModelIndex
永久索引:QPersistentModelIndex
如果要獲取一個數(shù)據(jù)項的index,必須具備三要素:行數(shù)藕漱,列數(shù)欲侮,父索引,下面會詳細介紹這三個屬性谴分。
Rows and columns
在一個最基本的形式中锈麸,一個model可以以一個簡單表格的方式,通過行列值來訪問牺蹄。這并不是說底層數(shù)據(jù)存放在一個數(shù)組中忘伞。利用行和列的數(shù)目只是一個約定,以允許組件相互通信沙兰。我們可以通過指定的行數(shù)和列數(shù)的模型檢索任何特定項目氓奈,并且得到代表數(shù)據(jù)項的index。
QModelIndex index = model->index(row, column, ...);
單一數(shù)據(jù)結(jié)構(gòu)的模型例如表格鼎天、列表不需要其他信息舀奶。但就像上面代碼表明,為了獲取索引我們需要提供更多的信息斋射。
Parents of items
由模型提供的類似表格項的數(shù)據(jù)接口對于表格或列表中的數(shù)據(jù)時理想的育勺;行列數(shù)目系統(tǒng)是一種準確映射數(shù)據(jù)的方式但荤。然而,像樹形的數(shù)據(jù)結(jié)構(gòu)需要模型更靈活的接口涧至。所以每個數(shù)據(jù)項又可以做為其他表的父項腹躁。
當我們請求一個模型項的索引時,我們必須提供父項的信息南蓬。在模型外纺非,唯一的查詢項目的方法就是通過索引,所以一個父項的索引是必須的:
QModelIndex index = model->index(row, column, parent);
Item roles
模型項可以為了其他的組件扮演不同的角色赘方,允許不同類型數(shù)據(jù)在不用的情況下被提供烧颖。例如: Qt::DisplayRole用于訪問一個字符串,這個字符串可以以一個文本的形式顯示在view中窄陡,通常炕淮,一個數(shù)據(jù)項包含多種不同的角色,標準的角色由Qt::ItemDataRole定義泳梆。
QVariant value = model->data(index, role);
對數(shù)據(jù)項最常用的用途都在 Qt::ItemDataRole中定義了鳖悠。通過為每個角色提供相應的數(shù)據(jù),模型可以提供hints优妙,并代表有關(guān)項目應如何呈現(xiàn)給用戶乘综。不同的views,可以自由地解釋或忽略此信息套硼。另外卡辰,也可以為應用程序特定的用途role完成附加的作用。
Summary
- 模型index為views和delegates提供了數(shù)據(jù)項的位置邪意,這樣做可以使他們遠離任何model內(nèi)在的數(shù)據(jù)結(jié)構(gòu)
- 數(shù)據(jù)項通過他們的行列數(shù)九妈,以及父項的索引值被查詢到
- 模型索引是應其他組件的要求由model創(chuàng)建的,例如views和delegates
- role區(qū)分與一個數(shù)據(jù)項相關(guān)的不同的數(shù)據(jù)類型
<u>Using model indexes</u>
為了展示我們怎樣在模型中獲取數(shù)據(jù)雾鬼,使用模型索引萌朱,我們建立一個QFileSystemModel,不用views在窗口中顯示策菜。