1. 程式人生 > >Qt之通用QAbstractTableModel資料框架

Qt之通用QAbstractTableModel資料框架

通用QAbstractTableModel資料框架ModelData

我們在使用Model/View框架的時候的時候總是需要根據不同的需求設計不同的資料Model。其實Table的資料其實就相當於一個二維的集合,所以我設計瞭如下的通用格式

環境

Qt5.6.2+Vs2013

程式碼

首先是對錶格的單元格進行抽象 QMyTableModelItem.h

#include <QMap>
#includeq<QVariant>

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<int, QVariant> 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  <QPair>

#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<int,int> //row, col
	QMap<QPair<int, int>, 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<int, int> _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<int, int> _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編寫