C++ QTableView中的大型数据集

C++ QTableView中的大型数据集,c++,qt,abstract-class,qabstractlistmodel,C++,Qt,Abstract Class,Qabstractlistmodel,我试图在QTableView中添加大量数据。由于存在大量数据,为了避免冻结应用程序,我尝试使用QAbstractTableModel不立即显示整个数据集,而是只显示必要的数据。基于此()的QT示例,我有以下类: FileListModel.cpp #include "filelistmodel.h" #include <QGuiApplication> #include <QDir> #include <QPalette> #include "qdebug.

我试图在QTableView中添加大量数据。由于存在大量数据,为了避免冻结应用程序,我尝试使用
QAbstractTableModel
不立即显示整个数据集,而是只显示必要的数据。基于此()的QT示例,我有以下类: FileListModel.cpp

#include "filelistmodel.h"

#include <QGuiApplication>
#include <QDir>
#include <QPalette>
#include "qdebug.h"

FileListModel::FileListModel(QObject *parent)
    : QAbstractTableModel(parent), fileCount(0) //edit
{}

int FileListModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : fileCount;
}

QVariant FileListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }
    if (index.row() >= fileList.size() || index.row() < 0)
    {
        return QVariant();
    }
    if (role == Qt::DisplayRole)
    {
        return fileList.at(index.row());
    }
    return QVariant();
}

bool FileListModel::canFetchMore(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return false;
    }
    return (fileCount < fileList.size());
}
int FileListModel::columnCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : colCount;
}
void FileListModel::fetchMore(const QModelIndex &parent)
{
    if (parent.isValid())
    {
        return;
    }
    int remainder = fileList.size() - fileCount;
    int itemsToFetch = qMin(100, remainder);

    if (itemsToFetch <= 0)
    {
        return;
    }
    beginInsertRows(QModelIndex(), fileCount, fileCount + itemsToFetch - 1);
    qDebug()<< "Qmodelindex "<< QModelIndex()<< "filecount "<< fileCount <<"filecount + itemtofetch "<<fileCount + itemsToFetch - 1;
    fileCount += itemsToFetch;

    endInsertRows();
}
void FileListModel::setColumnNumber(const int x) //edit
{
    colCount = x;
}
void FileListModel::setDataToList(const QList<float> &data)
{
    beginResetModel();
    fileList = data;
    fileCount = 0;
    endResetModel();
}

但是,添加的最后一个列表(
list5
)替换了前一个列表,我在所有列中只有相同的文本。我知道我需要编写所需的代码来添加列。但老实说,我不太了解
QAbstract
类,也不知道如何继续。如果您能提供一些提示或示例,说明如何修改代码以便在模型中添加列,我将不胜感激

我找到了将数据存储在QList>中的方法。 请在下面找到可能需要改进的工作代码:

CPP文件

#include "filelistmodel.h"

#include <QGuiApplication>
#include <QDir>
#include <QPalette>
#include "qdebug.h"

FileListModel::FileListModel(QObject *parent)
    : QAbstractTableModel(parent), rowNumber(0)
{}

int FileListModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : rowNumber;
}

int FileListModel::columnCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : colNumber;
}

void FileListModel::setColumnNumber(const int x)
{
    colNumber = x;
}

QVariant FileListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }
    if (index.row() >= fileList[0].size() || index.row() < 0)
    {
        return QVariant();
    }
    if (role == Qt::DisplayRole)
    {
        return fileList[index.column()][index.row()];
    }
    return QVariant();
}

bool FileListModel::canFetchMore(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return false;
    }
    return (rowNumber < fileList[0].size());
}

void FileListModel::fetchMore(const QModelIndex &parent)
{
    if (parent.isValid())
    {
        return;
    }
    int remainder = fileList[0].size() - rowNumber;
    int itemsToFetch = qMin(100, remainder);

    if (itemsToFetch <= 0)
    {
        return;
    }
    beginInsertRows(QModelIndex(), rowNumber, rowNumber + itemsToFetch - 1);

    rowNumber += itemsToFetch;

    endInsertRows();
}

void FileListModel::setDataToTable(const QList<QList<float>> &data)
{
    beginResetModel();
    fileList = data;
    rowNumber = 0;
    endResetModel();
}
#包括“filelistmodel.h”
#包括
#包括
#包括
#包括“qdebug.h”
FileListModel::FileListModel(QObject*parent)
:QAbstractTableModel(父级),行数(0)
{}
int FileListModel::行计数(常量QModelIndex&parent)常量
{
返回parent.isValid()?0:rowNumber;
}
int FileListModel::columnCount(常量QModelIndex&parent)常量
{
返回parent.isValid()?0:colNumber;
}
void FileListModel::setColumnNumber(常量int x)
{
colNumber=x;
}
QVariant FileListModel::data(常量QModelIndex&index,int角色)常量
{
如果(!index.isValid())
{
返回QVariant();
}
如果(index.row()>=fileList[0].size()|| index.row()<0)
{
返回QVariant();
}
如果(角色==Qt::DisplayRole)
{
返回文件列表[index.column()][index.row()];
}
返回QVariant();
}
bool FileListModel::canFetchMore(常量QModelIndex&parent)常量
{
if(parent.isValid())
{
返回false;
}
返回(rowNumbersetDataToTable(a);
FileListModel *model=new FileListModel(this);
model->setColumnNumber(5); //edit

model->setDataToList(list1);
model->setDataToList(list2);
model->setDataToList(list3);
model->setDataToList(list4);
model->setDataToList(list5);

tableview->setModel(model)
#include "filelistmodel.h"

#include <QGuiApplication>
#include <QDir>
#include <QPalette>
#include "qdebug.h"

FileListModel::FileListModel(QObject *parent)
    : QAbstractTableModel(parent), rowNumber(0)
{}

int FileListModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : rowNumber;
}

int FileListModel::columnCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : colNumber;
}

void FileListModel::setColumnNumber(const int x)
{
    colNumber = x;
}

QVariant FileListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }
    if (index.row() >= fileList[0].size() || index.row() < 0)
    {
        return QVariant();
    }
    if (role == Qt::DisplayRole)
    {
        return fileList[index.column()][index.row()];
    }
    return QVariant();
}

bool FileListModel::canFetchMore(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return false;
    }
    return (rowNumber < fileList[0].size());
}

void FileListModel::fetchMore(const QModelIndex &parent)
{
    if (parent.isValid())
    {
        return;
    }
    int remainder = fileList[0].size() - rowNumber;
    int itemsToFetch = qMin(100, remainder);

    if (itemsToFetch <= 0)
    {
        return;
    }
    beginInsertRows(QModelIndex(), rowNumber, rowNumber + itemsToFetch - 1);

    rowNumber += itemsToFetch;

    endInsertRows();
}

void FileListModel::setDataToTable(const QList<QList<float>> &data)
{
    beginResetModel();
    fileList = data;
    rowNumber = 0;
    endResetModel();
}
#ifndef FILELISTMODEL_H
#define FILELISTMODEL_H

#include <QAbstractListModel>
#include <QStringList>

class FileListModel : public QAbstractTableModel
{
    Q_OBJECT

public:
    FileListModel(QObject *parent = nullptr);

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    void setColumnNumber(const int );
    void setDataToTable(const QList<QList<float>>&);

protected:
    bool canFetchMore(const QModelIndex &parent) const override;
    void fetchMore(const QModelIndex &parent) override;

private:
    QList<QList<float>> fileList;
    int rowNumber;
    int colNumber;
};

#endif // FILELISTMODEL_H
FileListModel *pModel =new FileListModel(this);
QList<QList<float>> a;
pModel->setColumnNumber(5);
a.append(qlist1);
a.append(qlist2);
a.append(qlist3);
a.append(qlist4);
a.append(qlist5);
pModel->setDataToTable(a);