#ifndef CUSTOMITEMMODEL_H
#define CUSTOMITEMMODEL_H
#include <QAbstractItemModel>
#include"CustomItemModel.h"
#include<QDebug>
struct Student
{
int id;
QString name;
int age;
double score;
QString other;
};
class CustomItemModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit CustomItemModel(QObject *parent = nullptr);
~CustomItemModel();
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
virtual QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const override;
virtual QModelIndex parent(const QModelIndex &index) const override;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
virtual bool canFetchMore(const QModelIndex &parent) const override;
virtual void fetchMore(const QModelIndex &parent) override;
public:
inline QVector<bool > CheckState() {
return m_lastCheckState;
}
public slots:
void pushData(int id,QString name, int age,double score, QString other);
signals:
void signal_check(const QModelIndex &index, bool checked);
private:
QVector<Student> m_dataVector ;
QList<QString> m_headName;
QVector<QPair<bool, bool>> m_checked;
QVector<bool > m_lastCheckState;
};
#endif
#include "CustomItemModel.h"
#include <QDebug>
#include <QColor>
#include <QFont>
#include <iostream>
CustomItemModel::CustomItemModel(QObject *parent)
: QAbstractItemModel(parent)
{
Student s1;
s1.id = 1;
s1.age = 20;
s1.name = "Bob";
s1.score = 97;
s1.other = QStringLiteral("things");
m_dataVector.push_back(s1);
s1.id = 12;
s1.age = 20;
s1.name = "Bob";
s1.score = 97;
s1.other = QStringLiteral("things");
m_dataVector.push_back(s1);
s1.id = 102;
s1.age = 20;
s1.name = "Bob";
s1.score = 97;
s1.other = QStringLiteral("things");
m_dataVector.push_back(s1);
m_headName = { "ID","Name","Age","Score","Other" };
m_checked.append(QPair<bool, bool>(false, false));
m_checked.append(QPair<bool, bool>(false, false));
m_checked.append(QPair<bool, bool>(false, false));
for (size_t i = 0; i < 3; i++)
{
m_lastCheckState.push_back(false);
}
}
CustomItemModel::~CustomItemModel()
{
}
QVariant CustomItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Vertical)
return QVariant(section);
if (orientation == Qt::Horizontal)
{
if (section == 0)
return QVariant(m_headName[0]);
if (section == 1)
return QVariant(m_headName[1]);
if (section == 2)
return QVariant(m_headName[2]);
if (section == 3)
return QVariant(m_headName[3]);
if (section == 4)
return QVariant(m_headName[4]);
}
return QVariant();
}
bool CustomItemModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
if (value != headerData(section, orientation, role))
{
if (role != Qt::EditRole)
return false;
if (orientation == Qt::Vertical)
return false;
if (section == 0)
m_headName[0] = value.toString();
if (section == 1)
m_headName[1] = value.toString();
if (section == 2)
m_headName[2] = value.toString();
if (section == 3)
m_headName[3] = value.toString();
if (section == 4)
m_headName[4] = value.toString();
emit headerDataChanged(orientation, section, section);
return true;
}
return false;
}
QModelIndex CustomItemModel::index(int row, int column, const QModelIndex &parent) const
{
if (row > m_dataVector.size() - 1 || row < 0)
return QModelIndex();
if (column >4 || column < 0)
return QModelIndex();
return createIndex(row, column);
}
QModelIndex CustomItemModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}
int CustomItemModel::rowCount(const QModelIndex &parent) const
{
return m_dataVector.size();
}
int CustomItemModel::columnCount(const QModelIndex &parent) const
{
return 5;
}
bool CustomItemModel::hasChildren(const QModelIndex &parent) const
{
return false;
}
bool CustomItemModel::canFetchMore(const QModelIndex &parent) const
{
return false;
}
void CustomItemModel::fetchMore(const QModelIndex &parent)
{
}
QVariant CustomItemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() > m_dataVector.size() - 1 || index.row() < 0 || index.column() < 0 || index.column() > 4)
return QVariant();
Student s = m_dataVector.at(index.row());
switch (role)
{
case Qt::DisplayRole:
{
if (index.column() == 0)
{
return QVariant(s.id);
}
if (index.column() == 1)
{
return QVariant(s.name);
}
if (index.column() ==2)
{
return QVariant(s.age);
}
if (index.column() == 3)
{
return QVariant(s.score);
}
if (index.column() == 4)
{
return QVariant(s.other);
}
}
case Qt::TextAlignmentRole:
{
return Qt::AlignCenter;
}
case Qt::DecorationRole:
{
if (index.column() == 0)
return QColor(255, 0, 0, 255);
if (index.column() == 1)
return QColor(0, 255, 0, 255);
if (index.column() == 2)
{
return QColor(255, 255, 0, 255);
}
}
case Qt::ToolTipRole:
{
if (index.column() == 0)
return QString("this is first col");
if (index.column() == 1)
return QString("this is second col");
if (index.column() == 2)
return QString("this is 2 col");
}
case Qt::SizeHintRole:
{
return QVariant();
}
case Qt::FontRole:
{
QFont font;
if (index.column() == 0 ? m_checked.at(index.row()).first : m_checked.at(index.row()).second)
{
font.setBold(true);
return font;
}
else
{
return font;
}
}
case Qt::BackgroundRole:
{
return QVariant();
}
case Qt::ForegroundRole:
{
return QVariant();
}
case Qt::CheckStateRole:
{
if (index.column() == 0)
{
if (m_checked.at(index.row()).first)
return Qt::Checked;
else
return Qt::Unchecked;
}
else
{
if (m_checked.at(index.row()).second)
return Qt::Checked;
else
return Qt::Unchecked;
}
}
case Qt::UserRole + 1:
{
return QVariant();
}
default:
return QVariant();
}
}
bool CustomItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (data(index, role) != value)
{
switch (role)
{
case Qt::EditRole:
{
if (value.toString().isEmpty())
{
return true;
}
Student s = m_dataVector.at(index.row());
if (index.column() == 0)
{
s.id = value.toInt();
}
if (index.column() == 1)
{
s.name = value.toString();
}
if (index.column() ==2)
{
s.age = value.toInt();
}
if (index.column() == 3)
{
s.score = value.toDouble();
}
if (index.column() == 4)
{
s.other = value.toString();
}
m_dataVector.replace(index.row(), s);
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
case Qt::CheckStateRole:
{
#if 1
QPair<bool,bool> cheked = m_checked.at(index.row());
emit signal_check(index, value.toBool());
if (index.column()==4)
{
m_lastCheckState.replace(index.row(), value.toBool());
}
if (index.column() == 0)
{
m_checked.replace(index.row(), QPair<bool, bool>(value.toBool(), cheked.second));
}
else
{
m_checked.replace(index.row(), QPair<bool, bool>(cheked.first, value.toBool()));
}
emit dataChanged(index, index, QVector<int>() << role);
return true;
#endif
}
default:
return false;
}
}
return false;
}
Qt::ItemFlags CustomItemModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
}
bool CustomItemModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (row<0 || row>m_dataVector.size() || count < 1)
return false;
beginInsertRows(parent, row, row + count - 1);
for (quint8 i = 0; i < count; i++)
{
Student s1;
s1.age = 0;
s1.id = 0;
s1.name = "";
s1.other = "";
s1.score = 0;
QPair<bool, bool> state{ false,false };
m_dataVector.insert(row + i, s1);
m_checked.insert(row + i, state);
}
endInsertRows();
return true;
}
bool CustomItemModel::insertColumns(int column, int count, const QModelIndex &parent)
{
beginInsertColumns(parent, column, column + count - 1);
endInsertColumns();
return false;
}
bool CustomItemModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (row<0 || row>m_dataVector.size() || count < 1)
return false;
beginRemoveRows(parent, row, row + count - 1);
for (quint8 i = 0; i < count; i++)
{
if (m_dataVector.size() == 0)
return true;
m_dataVector.remove(row);
m_checked.remove(row);
}
endRemoveRows();
return true;
}
bool CustomItemModel::removeColumns(int column, int count, const QModelIndex &parent)
{
beginRemoveColumns(parent, column, column + count - 1);
endRemoveColumns();
return false;
}
void CustomItemModel:: pushData(int id, QString name, int age, double score, QString other)
{
Student s;
s.id = id;
s.name = name;
s.age = age;
s.score = score;
s.other = other;
beginInsertRows(QModelIndex(), m_dataVector.size(), m_dataVector.size());
m_dataVector.push_back(s);
m_checked.push_back(QPair<bool, bool>(false, false));
endInsertRows();
}