**
QAbstractItemModel
**
1)当QAbstractItemModel还没有内容时,data() 函数不会响应dataChanged信号;
2)当QAbstractItemModel调用 appendRow方法后,会自动调用 data();
3)只有当QAbstractItemModel有内容时,data()才会响应dataChanged()。dataChanged()的前两个变量分别表示数据变化区域的左上角和右下角。事实上,一个dataChanged()会触发data()好几次,不仅会修改响应单元格的内容,也会改变其背景色等。
QStandartItemModel说明:
https://blog.youkuaiyun.com/bluewhu/article/details/104924809
优点
使用QStandardItemModel表示数据集具有以下优点:该类使用QStandardItem存放数据项,用户不必定义任何数据结构来存放数据项;QStandardItem使用自关联关系,能够表达列表、表格、树甚至更复杂的数据结构,能够涵盖各种各样的数据集;QStandardItem本身存放着多个『角色,数据子项』,视图类、委托类或者其他用户定义的类能够方便地依据角色访问各个数据子项。
缺点
然而,这种表示方法也有局限性:当数据集中的数据项很多时,施加在数据集上的某些操作的执行效率会很低。比如,设数据集是一个1万行、20列的表格,其中第10列存放的是浮点数。如果我们想计算这一列的平均值,按照图13 14,这需要遍历所有行,取得第10列的QStandardItem对象,再依据角色“Qt::DisplayRole”取得对应的数据子项。由于这个数据子项的类型为QString,还需要将其转换为浮点数,最后求所有浮点数的平均值。这些操作会耗费较长的时间。
结论
因此,对于数据量不是很大、对性能要求不是很高的场合,我们可以使用类QStandardItemModel来表示一个数据集。否则,用户应该从QAbstractItemModel、QAbstractListModel或者QAbstractTableModel派生新类,自行管理数据集的存放与访问。
#ifndef CUSTOMITEMMODEL_H
#define CUSTOMITEMMODEL_H
#include <QAbstractItemModel>
#include <QVector>
#include <QMap>
class CustomItemModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit CustomItemModel(QObject* parent = nullptr);
~CustomItemModel();
// Header:
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role = Qt::EditRole) override;
// Basic functionality:
QModelIndex index(int row, int column,
const QModelIndex& parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex& index) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
// Fetch data dynamically:
bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex& parent) const override;
void fetchMore(const QModelIndex& parent) override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
// Editable:
bool setData(const QModelIndex& index, const QVariant& value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
// Add data:
bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
bool insertColumns(int column, int count, const QModelIndex& parent = QModelIndex()) override;
// Remove data:
bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
bool removeColumns(int column, int count, const QModelIndex& parent = QModelIndex()) override;
public slots:
void pushData(QString, int); //向模型添加一个数据
public:
QVector<QPair<QString, int>>* m_dataVector = nullptr; //新建一个储存数据的序列对象,每个元素(行)两个值,一个储存姓名、一个储存年龄
QList<QString> m_headName; //储存表头名称
QVector<QPair<bool, bool>> m_checked{
}; //储存某个位置是否被勾选
};
#endif
custom_item_model.cpp
#include "custom_item_model.h"
#include <QDebug>
#include <QColor>
#include <QFont>
#include