(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)
谢谢