(1)字符串列表型的 model ,可以交給視圖 view 來顯示,也可以由組合框 comboBox 讀取其中的內容 :
(2)以下開始學習本字符串 model 里的成員函數,本類沒有再定義信號與槽函數 :
++
++ 依然是基于同一個例子來做實驗 :
++ 獲得本模型中條目的標志 :
(3)讀取與設置本模型里的條目里的數據 :
++拿本節的子模型測試一下 :
++補充,其實本類重新實現了基類里以上的三個成員函數的 :
++ 以下是本列表類里的成員函數 :
++測試 :
++ 再測試 :
++ 奇奇怪怪的角色數據 :
(4)返回與形參 3的索引在同一父類下的位于 (row, column) 坐標處的同伴的索引。故形參 3不可為空 :
(5)模型里元素條目的插入與刪除 :
++測試 :
(6)模型內行的移動 :
++ 給出測試 :
(7)拖動模型里的條目 :
(8)給出本類的繼承關系 :
(9)至此,本字符串列表模型 QStringListModel 的源碼閱讀完畢,給出帶了一些注釋的源碼,本類定義在頭文件 qstringlistmodel . h :
#ifndef QSTRINGLISTMODEL_H
#define QSTRINGLISTMODEL_H#include <QtCore/qabstractitemmodel.h>
#include <QtCore/qstringlist.h>QT_REQUIRE_CONFIG(stringlistmodel);QT_BEGIN_NAMESPACE/*
The QStringListModel class provides a model that supplies strings to views.QStringListModel是一種可編輯的模型,適用于簡單場景,
即需要在視圖小部件(如 QListView 或 QComboBox)中顯示一系列字符串。
QStringListModel is an editable model that can be used for simple caseswhere you need to display a number of strings in a view widget,such as a QListView or a QComboBox.該模型提供了可編輯模型的所有標準功能,它將字符串列表中的數據表示為一個具有一列且行數等于列表中項目數量的模型。
The model provides all the standard functions of an editable model,
representing the data in the string list as a model with one column anda number of rows equal to the number of items in the list.模型索引對應于項目通過index()函數獲得,項目標志通過flags()獲得。
項目數據通過data()函數讀取,并使用setData()寫入。
行數(以及字符串列表中的項目數量)可以通過rowcount()函數找到。
Model indexes corresponding to items are obtained with the index() function,
and item flags are obtained with flags().
Item data is read with the data() function and written with setData().
The number of rows (and number of items in the string list)can be found with the rowCount() function.模型可以使用現有的字符串列表進行構建,或者可以稍后使用setstringList()便捷函數設置字符串列表。
字符串也可以通過insertRows()函數以常規方式插入,并使用removeRows()移除。
可以通過stringList()便利函數獲取字符串列表的內容。
The model can be constructed with an existing string list,
or strings can be set later with the setStringList() convenience function.
Strings can also be inserted in the usual way with the insertRows() function,
and removed with removeRows().
The contents of the string list can be retrieved with the stringList() convenience function.*/class Q_CORE_EXPORT QStringListModel : public QAbstractListModel
{Q_OBJECTprivate:Q_DISABLE_COPY(QStringListModel)QStringList lst; //本類的數據成員 using QStringList = QList<QString>;public://Constructs a string list model with the given parent.explicit QStringListModel( QObject * parent = nullptr);explicit QStringListModel(const QStringList & strings, QObject * parent = nullptr);//Constructs a string list model containing the specified strings with the given parent.//Returns the string list used by the model to store data.QStringList stringList() const;void setStringList(const QStringList & strings); //本函數會觸發信號以更新綁定的視圖//Sets the model's internal string list to strings.//The model will notify any attached views that its underlying data has changed.//enum Qt::SortOrder { AscendingOrder, DescendingOrder };void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;//Reimplements: QAbstractItemModel::sort(int column, Qt::SortOrder order).//Reimplements: QAbstractItemModel::rowCount(const QModelIndex & parent) const.//返回模型中行的數量。該值對應于模型內部字符串列表中的項目數量。//在大多數模型中,可選的父項參數 parent 用于指定要計數的行的父項。// 由于這是一個列表,如果指定了有效的父項,則結果將始終為0。int rowCount(const QModelIndex & parent = QModelIndex()) const override;/*enum Qt::ItemFlag {NoItemFlags = 0,ItemIsSelectable = 1,ItemIsEditable = 2,ItemIsDragEnabled = 4,ItemIsDropEnabled = 8,ItemIsUserCheckable = 16,ItemIsEnabled = 32,ItemIsAutoTristate = 64,ItemNeverHasChildren = 128,ItemIsUserTristate = 256};Q_DECLARE_FLAGS(ItemFlags, ItemFlag)*/Qt::ItemFlags flags(const QModelIndex & index) const override;//Reimplements: QAbstractListModel::flags(const QModelIndex &index) const.//Returns the flags for the item with the given index.//Valid items are enabled, selectable, editable, drag enabled and drop enabled.//Reimplements: QAbstractItemModel::data(const QModelIndex &index, int role) const.//Returns data for the specified role, from the item with the given index.//If the view requests an invalid index, an invalid variant is returned.QVariant data(const QModelIndex & index, // Qt::DisplayRole = 0int role = Qt::DisplayRole) const override;//QVariant QModelIndex::data(int role = Qt::DisplayRole)bool setData(const QModelIndex & index, //本函會觸發模型的 dataChanged()信號const QVariant & value, // Qt::EditRole = 2int role = Qt::EditRole ) override;//Reimplements: QAbstractItemModel::setData(// QModelIndex & index, QVariant & value, int role).//Sets the data for the specified role in the item with the given index in the model,// to the provided value.//The dataChanged() signal is emitted if the item is changed.//Returns true after emitting the dataChanged() signal.//void QAbstractItemModel::dataChanged( QModelIndex & topLeft,// QModelIndex & bottomRight, QList<int> & roles = QList<int>());//Reimplements: QAbstractItemModel::itemData(const QModelIndex & index) const.QMap<int, QVariant> itemData(const QModelIndex & index) const override;//Reimplements: QAbstractItemModel::setItemData(index, QMap<int, QVariant> & roles).//If roles contains both Qt::DisplayRole = 0 and Qt::EditRole = 2,// the latter will take precedence。 //EditRole具有更高優先級(而 DecorationRole = 1)bool setItemData(const QModelIndex & index,const QMap<int, QVariant> & roles) override;bool clearItemData(const QModelIndex & index) override;//本函也會觸發 dataChanged()信號//Reimplements: QAbstractItemModel::clearItemData(const QModelIndex & index).//Removes the data stored in all the roles for the given index. 刪除成功則返回 true。//返回與形參 3的索引在同一父類下的位于 (row, column) 坐標處的同伴的索引。故形參 3不可為空。//Reimplements: QAbstractListModel::sibling(int row, int column, QModelIndex & idx).QModelIndex sibling(int row, int column, const QModelIndex & idx) const override;//模型索引也有檢測同伴的成員函數 QModelIndex QModelIndex::sibling(int row, int column)//Reimplements: QAbstractItemModel::insertRows(int row, int count, QModelIndex & parent).//Inserts count rows into the model, beginning at the given row.//The parent index of the rows is optional// and is only used for consistency with QAbstractItemModel.//By default, a null index is specified,// indicating that the rows are inserted in the top level of the model.//Returns true if the insertion was successful.//對于本字符串列表模型來講,形參 3 的父節點索引可為空,因為插入的都是頂層節點。bool insertRows(int row, int count, //在本列表里的下表 row 行處插入 count 行條目。const QModelIndex & parent = QModelIndex()) override;bool removeRows(int row, int count, //刪除本列表里從行 row 開始的 count 個條目const QModelIndex & parent = QModelIndex()) override;//Reimplements: QAbstractItemModel::removeRows(int row, int count, QModelIndex & parent).//Removes count rows from the model, beginning at the given row.//The parent index of the rows is optional// and is only used for consistency with QAbstractItemModel.//By default, a null index is specified, indicating that the// rows are removed in the top level of the model.//Returns true if the row removal was successful.//以下這兩個成員函數繼承于其基類。這倆函數,也是可以用的。//bool QAbstractItemModel::insertRow(int row, QModelIndex & parent = QModelIndex())//bool QAbstractItemModel::removeRow(int row, QModelIndex & parent = QModelIndex())bool moveRows (const QModelIndex & sourceParent, int sourceRow, int count,const QModelIndex & destinationParent, int destinationChild) override;//QAbstractItemModel::moveRows(QModelIndex & sourceParent, int sourceRow, int count,//本類重新實現了基類里的同一函數 QModelIndex & destinationParent, int destinationChild).//以下這個成員函數繼承于其基類。也是可以用的。一次只移動模型里的一行元素。//bool moveRow (const QModelIndex & sourceParent , int sourceRow,// const QModelIndex & destinationParent, int destinationChild)Qt::DropActions supportedDropActions() const override; //描述拖動模型條目時的語義//Reimplements: QAbstractItemModel::supportedDropActions() const./*enum Qt::DropAction { //本枚舉類用于描述模型視圖里的拖動操作的語義:復制、剪切或超鏈接。CopyAction = 0x 1, //Copy the data to the target.MoveAction = 0x 2, //Move the data from the source to the target.LinkAction = 0x 4, //Create a link from the source to the target.ActionMask = 0x ff,TargetMoveAction = 0x8002, //在 Windows上,當 D&D數據的所有權應被目標應用程序接管時,//即源應用程序不應刪除這些數據時,會使用此值。//在X11上,此值用于執行移動操作。Mac上不使用TargetMoveAction。IgnoreAction = 0x 0 //Ignore the action (do nothing with the data).};Q_DECLARE_FLAGS(DropActions, DropAction)Q_DECLARE_OPERATORS_FOR_FLAGS(DropActions)*/}; //完結 class QStringListModel : public QAbstractListModelQT_END_NAMESPACE#endif // QSTRINGLISTMODEL_H
(10)
謝謝