我有一个问题,我的QtableView在插入100.000行并特别使用多个SELECT之后变得非常缓慢。
因此,在进行一些重新搜索之后,我决定使用自己的模型,该模型从qabstractitemmodel继承。但是问题是我不知道如何使用它,因为文本文件可以更改。fx。我可以加载一个有5列和50.000行的文件,一段时间后,它可以有15列和10.000行。数据不需要更改,因此仅读取。有人可以帮助我解决这个问题吗?
我的"空"自定义模型在这里
#include "customabstractmodel.h"
CustomAbstractModel::CustomAbstractModel(QObject *parent)
: QAbstractItemModel(parent)
{
}
QVariant CustomAbstractModel::headerData(int section, Qt::Orientation orientation, int role) const
{
// FIXME: Implement me!
}
bool CustomAbstractModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
if (value != headerData(section, orientation, role)) {
// FIXME: Implement me!
emit headerDataChanged(orientation, section, section);
return true;
}
return false;
}
QModelIndex CustomAbstractModel::index(int row, int column, const QModelIndex &parent) const
{
// FIXME: Implement me!
}
QModelIndex CustomAbstractModel::parent(const QModelIndex &index) const
{
// FIXME: Implement me!
}
int CustomAbstractModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return 0;
// FIXME: Implement me!
}
int CustomAbstractModel::columnCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return 0;
// FIXME: Implement me!
}
QVariant CustomAbstractModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
// FIXME: Implement me!
return QVariant();
}
bool CustomAbstractModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (data(index, role) != value) {
// FIXME: Implement me!
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags CustomAbstractModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable; // FIXME: Implement me!
}
bool CustomAbstractModel::insertRows(int row, int count, const QModelIndex &parent)
{
beginInsertRows(parent, row, row + count - 1);
// FIXME: Implement me!
endInsertRows();
}
bool CustomAbstractModel::insertColumns(int column, int count, const QModelIndex &parent)
{
beginInsertColumns(parent, column, column + count - 1);
// FIXME: Implement me!
endInsertColumns();
}
bool CustomAbstractModel::removeRows(int row, int count, const QModelIndex &parent)
{
beginRemoveRows(parent, row, row + count - 1);
// FIXME: Implement me!
endRemoveRows();
}
bool CustomAbstractModel::removeColumns(int column, int count, const QModelIndex &parent)
{
beginRemoveColumns(parent, column, column + count - 1);
// FIXME: Implement me!
endRemoveColumns();
}
编辑#1 CSV数据(不是真实信息,随机化)
名称,电话,电子邮件,地址,城市,邮政编码Chavez,Lisandra F.,1-498-913-8181,ac.fermentum.vel@semperrutrumfusce.org,AP#123-1044 SED Rd。,Drayton Valley,21833Humphrey,Briar W.,1-583-466-4027,Morbi.accumsan.laoreet@loremipsumdolor.net,AP#642-6497 ID Rd。,Lochgilphead,16394Benson,Tasha H.,1-898-918-7872,Reactat@Enimetiam.ca,P.O。Box 197,4720 Ipsum。街,特尔福德,56688艾默生(Emerson),苏珊(Susan P.),1-190-818-1919,dignissim@liberoatauctor.ca,Box 482,7813 Dolor。大街,圣安东尼奥,M8C 7F6Dunn,Alexander U.,1-222-379-2231,Libero.donec.consectetuer@nonegestasa.ca,803-958 Lectus Rd。,Raleigh,74078
假设我们将整个文件读为内存,我将通过以下方式实现模型(QAbstractTableModel
的子类):
CustomAbstractModel::CustomAbstractModel(const QString &filePath,
QObject *parent)
:
QAbstractTableModel(parent)
{
// Parse the file and store its data in an internal data structure
QFile file(filePath);
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
while (!in.atEnd())
{
// Assuming that m_data is a std::vector<QString>
m_data.emplace_back(in.readLine());
}
}
}
QVariant CustomAbstractModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole &&
!m_data.empty())
{
// Read the column names from the first line
const QString &firstLine = m_data.front();
QStringList headers = firstLine.split(',');
return headers.at(section);
}
}
int CustomAbstractModel::rowCount(const QModelIndex &parent) const
{
if (!m_data.empty())
{
return m_data.size() - 1; // First line is the column names data
}
return 0;
}
int CustomAbstractModel::columnCount(const QModelIndex &parent) const
{
if(!m_data.empty())
{
const QString &firstLine = m_data.front();
return firstLine.count(",") + 1;
}
return 0;
}
QVariant CustomAbstractModel::data(const QModelIndex &index, int role) const
{
if (m_data.empty() && role == Qt::DisplayRole)
{
// First line is the header data.
const QString &line = m_data[index.row() + 1];
QStringList columnData = firstLine.split(',');
return columnData.at(index.column());
}
return QVariant();
}