在上一篇文章 - QTableView效率优化2中https://blog.csdn.net/qq_37996632/article/details/123551325?spm=1001.2014.3001.5501,继承QAbstractTableModel自定义了一个简陋的table model,虽然能快速加载出10w,100w级别的数据,但是本身并没有任何接口。想要这个model正常使用,一般来说还需要加上这么几个接口:
- setItemText() - 为指定的单元格(QModelIndex/row,column)设置文字;
- itemText() - 获取指定的单元格(QModelIndex/row,column)的文字;
- setItemData() - 为指定的单元格(QModelIndex/row,column)设置data(QVariant);
- itemData() - 获取指定的单元格(QModelIndex/row,column)的data(QVariant);
- setHignlightIndex() - 为指定的单元格设置背景色;
- clear() - 清空model中的所有数据
以及信号:
- itemChanged(const QModelIndex &index, const QVariant &value);
nari_tableitemmodel.h
#ifndef NARI_TABLEITEMMODEL_H
#define NARI_TABLEITEMMODEL_H
#include <QAbstractTableModel>
#include <QObject>
#include <QStringList>
#include <QVector>
class NARI_TableItemModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit NARI_TableItemModel(QObject *parent = NULL);
~NARI_TableItemModel();
void SetData(const QVariantMap &map);
void setHignlightIndex(QVector< QPair<int, int> > vec_index);
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
virtual int rowCount(const QModelIndex &parent) const;
virtual int columnCount(const QModelIndex &parent) const;
virtual QVariant data(const QModelIndex &index, int role) const;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
QString itemText(int row, int column) const;
QString itemText(const QModelIndex &index) const;
void setItemText(const QModelIndex &index, const QString &str);
void setItemText(int row, int column, const QString &str);
QVariant ItemData(const QModelIndex &index) const;
QVariant ItemData(int row, int column) const;
void SetItemData(const QModelIndex &index, const QVariant &data);
void SetItemData(int row, int column, const QVariant &data);
void clear();
signals:
void itemChanged(const QModelIndex &index, const QVariant &value);
private:
QStringList m_hor_hedlbls; // headerlabels
QStringList m_vec_hedlbls; // oids - map.keys()
QVariantMap m_table_map, m_data_map;
QVector<QPair<int, int> > m_highlight_indexs; // 背景高亮的indexs
};
#endif // NARI_TABLEITEMMODEL_H
nari_tableitemmodel.cpp
#include "nari_tableitemmodel.h"
#include <QDebug>
#include <QColor>
#include <QIcon>
NARI_TableItemModel::NARI_TableItemModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
NARI_TableItemModel::~NARI_TableItemModel()
{
}
void NARI_TableItemModel::SetData(const QVariantMap &map)
{
beginResetModel();
m_hor_hedlbls = map["headerlabel"].toStringList();
m_table_map = map;
m_vec_hedlbls = map.keys();
endResetModel();
}
void NARI_TableItemModel::setHignlightIndex(QVector<QPair<int, int> > vec_index)
{
beginResetModel();
m_highlight_indexs = vec_index;
endResetModel();
}
Qt::ItemFlags NARI_TableItemModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
flags |= Qt::ItemIsEditable;
return flags;
}
QVariant NARI_TableItemModel::data(const QModelIndex &_index, int role) const
{
if(role == Qt::DisplayRole || role == Qt::EditRole)
{
QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[_index.row()]].data());
return (*_map)[m_hor_hedlbls[_index.column()]];
}
else if(role == Qt::TextAlignmentRole) return Qt::AlignCenter; // 文字居中
else if(role == Qt::BackgroundColorRole) // 设置背景色
{
if(m_highlight_indexs.contains(qMakePair<int, int>(_index.row(), _index.column())))
return QColor(230, 247, 255);
}
// else if(role == Qt::DecorationRole) // 为item添加图标
// {
// return QIcon(":/images/device.svg");
// }
return QVariant();
}
QVariant NARI_TableItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal)
{
if (role == Qt::DisplayRole) return m_hor_hedlbls.at(section);
else return QVariant();
}
return QAbstractTableModel::headerData(section, orientation, role); // 垂直表头的序号
}
bool NARI_TableItemModel::setData(const QModelIndex &_index, const QVariant &value, int role)
{
if (_index.isValid() && role == Qt::EditRole)
{
QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[_index.row()]].data());
if((*_map)[m_hor_hedlbls[_index.column()]] != value)
{
(*_map)[m_hor_hedlbls[_index.column()]] = value;
emit dataChanged(_index, _index);
emit itemChanged(_index, value);
return true;
}
}
return false;
}
int NARI_TableItemModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) return 0;
else return m_table_map.size() - 2;
}
int NARI_TableItemModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid()) return 0;
else return m_hor_hedlbls.size();
}
QString NARI_TableItemModel::itemText(int row, int column) const
{
QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[row]].data());
return (*_map)[m_hor_hedlbls[column]].toString();
}
QString NARI_TableItemModel::itemText(const QModelIndex &index) const
{
QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[index.row()]].data());
return (*_map)[m_hor_hedlbls[index.column()]].toString();
}
void NARI_TableItemModel::setItemText(const QModelIndex &index, const QString &str)
{
QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[index.row()]].data());
(*_map)[m_hor_hedlbls[index.column()]] = str;
}
void NARI_TableItemModel::setItemText(int row, int column, const QString &str)
{
QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[row]].data());
(*_map)[m_hor_hedlbls[column]] = str;
}
QVariant NARI_TableItemModel::ItemData(const QModelIndex &index) const
{
QVariantMap *_map = (QVariantMap*)(m_data_map[m_vec_hedlbls[index.row()]].data());
return (*_map)[m_hor_hedlbls[index.column()]];
}
QVariant NARI_TableItemModel::ItemData(int row, int column) const
{
QVariantMap *_map = (QVariantMap*)(m_data_map[m_vec_hedlbls[row]].data());
return (*_map)[m_hor_hedlbls[column]];
}
void NARI_TableItemModel::SetItemData(const QModelIndex &index, const QVariant &data)
{
QString vec_lbl = m_vec_hedlbls[index.row()];
if(!m_data_map.contains(vec_lbl)) m_data_map[vec_lbl] = QVariantMap();
QVariantMap *_map = (QVariantMap*)(m_data_map[vec_lbl].data());
(*_map)[m_hor_hedlbls[index.column()]] = data;
}
void NARI_TableItemModel::SetItemData(int row, int column, const QVariant &data)
{
QString vec_lbl = m_vec_hedlbls[row];
if(!m_data_map.contains(vec_lbl)) m_data_map[vec_lbl] = QVariantMap();
QVariantMap *_map = (QVariantMap*)(m_data_map[vec_lbl].data());
(*_map)[m_hor_hedlbls[column]] = data;
}
void NARI_TableItemModel::clear()
{
beginResetModel();
m_table_map.clear();
m_data_map.clear();
m_hor_hedlbls.clear();
m_vec_hedlbls.clear();
m_highlight_indexs.clear();
endResetModel();
}
mainwindow.cpp
// 填充数据
QVariantMap data_map;
data_map["headerlabel"] = QStringList() << "ID" << tr("jiange") << tr("device") << tr("device type") << tr("factory name") << tr("voltage type") << tr("wf identifier") << tr("wf tuichu");
data_map["headerenum"] = QVariantList() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
for(int i = 0; i < 100000; i++)
{
QVariantMap map_1;
map_1["ID"] = i + 1;
map_1["jiange"] = i + 2;
map_1["device"] = i + 3;
map_1["device type"] = i + 4;
map_1["factory name"] = i + 5;
map_1["voltage type"] = i + 6;
map_1["wf identifier"] = i + 7;
map_1["wf tuichu"] = i + 8;
data_map[QString::number(i) + "id"] = map_1;
}
table_model->SetData(data_map);
// 测试setHignlightIndex
void MainWindow::on_btn_hignlight_clicked()
{
QVector< QPair<int, int> > indexs;
for(int i = 0; i < 2000; i++)
{
indexs.append(qMakePair<int ,int>(i, 2));
}
table_model->setHignlightIndex(indexs);
}
// 测试setItemText,SetItemData()
void MainWindow::on_btn_setItemText_clicked()
{
table_model->setItemText(1, 1, "test");
table_model->SetItemData(1, 1, "aoligei");
}
效果演示如下:
到此为止基本的功能都有了,加载效率也很快,不过还有个问题,内存占用会非常大,一百万行数据甚至能占用1个多g的内存。这点可以通过QContiguousCache来优化,下一篇将会针对QContiguousCache进行内容的补充。