我们在使用Model/View框架的时候的时候总是需要根据不同的需求设计不同的数据Model。其实Table的数据其实就相当于一个二维的集合,所以我设计了如下的通用格式
Qt5.6.2+Vs2013
首先是对表格的单元格进行抽象
QMyTableModelItem.h
#include
#includeq
class QMyTableModelItem
{
public:
QMyTableModelItem(int row, int col);
~QMyTableModelItem();
//设置参数
void setItemData(const QVariant& data, int role = Qt::DisplayRole);
//获取参数
QVariant data(int role = Qt::DisplayRole);
private:
int x_row; //行
int x_col; //列
//key: role
QMap x_roleData;
};
QMyTableModelItem.cpp
#include "QMyTableModelItem.h"
QMyTableModelItem::QMyTableModelItem(int row, int col)
: x_row(row)
, x_col(col)
{
x_roleData.clear();
}
QMyTableModelItem::~QMyTableModelItem()
{
x_roleData.clear();
}
void QMyTableModelItem::setItemData(const QVariant& data, int role /*= Qt::DisplayRole*/)
{
auto _iter = x_roleData.find(role);
if (_iter != x_roleData.end())
{
//存在
_iter.value() = data;
}
else
{
x_roleData.insert(role, data);
}
}
QVariant QMyTableModelItem::data(int role /*= Qt::DisplayRole*/)
{
auto _iter = x_roleData.find(role);
if (_iter != x_roleData.end())
{
//存在
return _iter.value();
}
return QVariant();
}
其次建立表格模型
QMyTableModelData.h
#include
#include "QMyTableModelItem.h"
class QMyTableModelData : public QObject
{
Q_OBJECT
public:
explicit QMyTableModelData(QObject *parent = nullptr);
~QMyTableModelData();
//设置模型参数
void setModelData(int row, int col, const QVariant& data, int role = Qt::DisplayRole);
//获取参数
QVariant data(int row, int col, int role = Qt::DisplayRole);
//函数
int rowCount() const;
//列数
int columnCount() const;
private:
int x_rowCnt; //行数
int x_colCnt; //列数
//QPair //row, col
QMap, QMyTableModelItem> x_modelDatas;
};
QMyTableModelData.cpp
#include "QMyTableModelData.h"
QMyTableModelData::QMyTableModelData(QObject *parent)
: QObject(parent)
{
x_rowCnt = x_colCnt = 0;
x_modelDatas.clear();
}
QMyTableModelData::~QMyTableModelData()
{
x_modelDatas.clear();
}
void QMyTableModelData::setModelData(int row, int col, const QVariant& data, int role /*= Qt::DisplayRole*/)
{
if (x_rowCnt <= row)
{
x_rowCnt = row + 1;
}
if (x_colCnt <= col)
{
x_colCnt = col + 1;
}
QPair _pair(row, col);
auto _iter = x_modelDatas.find(_pair);
if (_iter != x_modelDatas.end())
{
//存在
QMyTableModelItem& _item = _iter.value();
_item.setItemData(data, role);
}
else
{
//不存在
QMyTableModelItem _item(row, col);
_item.setItemData(data, role);
x_modelDatas.insert(_pair, _item);
}
}
QVariant QMyTableModelData::data(int row, int col, int role /*= Qt::DisplayRole*/)
{
QPair _pair(row, col);
auto _iter = x_modelDatas.find(_pair);
if (_iter != x_modelDatas.end())
{
//存在
QMyTableModelItem _item = _iter.value();
return _item.data(role);
}
return QVariant();
}
int QMyTableModelData::rowCount() const
{
return x_rowCnt;
}
int QMyTableModelData::columnCount() const
{
return x_colCnt;
}
这样可以用QMyTableModelData类作为Model的数据存储,进行Model/View编写
x_pModelData = new QMyTableModelData(this);
x_pModelData->setModelData(0, 0, 1);
x_pModelData->setModelData(0, 0, true, Wsd::CheckRole);
x_pModelData->setModelData(0, 0, QVariant(Qt::AlignCenter), Qt::TextAlignmentRole);
x_pModelData->setModelData(0, 1, QString::fromLocal8Bit("张三"));
x_pModelData->setModelData(0, 1, QColor(Qt::red), Qt::ForegroundRole);
x_pModelData->setModelData(0, 2, 16);
x_pModelData->setModelData(1, 0, 2);
x_pModelData->setModelData(1, 0, QVariant(Qt::AlignCenter), Qt::TextAlignmentRole);
x_pModelData->setModelData(1, 1, QString::fromLocal8Bit("李四"));
x_pModelData->setModelData(1, 2, 16);
然后在自己的model类中使用
QVariant QCustomTableModel::data(const QModelIndex &index, int role /*= Qt::DisplayRole*/) const
{
if (!index.isValid())
{
return QVariant();
}
return x_pModelData->data(index.row(), index.column(), role);
}