Qt MVC model 加载 大量数据的性能优化

您所在的位置:网站首页 有更多的数据可用 Qt MVC model 加载 大量数据的性能优化

Qt MVC model 加载 大量数据的性能优化

2024-05-25 00:52| 来源: 网络整理| 查看: 265

Qt MVC model 加载 大量数据的性能优化 自定义 canFetchMore() 、fetchMore()

canFetchMore()函数的作用是:检查父节点是否有更多可用的数据,并相应地返回true或false。fetchMore()函数的作用是:根据指定的父对象获取数据。例如,可以在涉及增量数据的数据库查询中组合这两个函数,以填充QAbstractItemModel。我们重新实现canFetchMore()来指示是否有更多的数据需要获取,并根据需要fetchMore()来填充模型。 例如动态填充树模型,当树模型中的一个分支展开时,我们将重新实现。 如果fetchMore()的重新实现向模型中添加了行,则需要调用beginInsertRows()和endInsertRows()。同样,canFetchMore()和fetchMore()都必须被重新实现。

filelistmodel.h #ifndef FILELISTMODEL_H #define FILELISTMODEL_H #include #include #include //![0] class FileListModel : public QAbstractListModel { Q_OBJECT public: FileListModel(QObject *parent = 0); int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; signals: ///通知外部,新获取了多少行 void numberPopulated(int number); public slots: ///设置模型数据 void setDirPath(const QString &path); ///设置 fetchMore每次加载的最大数据行数 void setPAGE_ITEM_COUNT(int value); protected: bool canFetchMore(const QModelIndex &parent) const override; void fetchMore(const QModelIndex &parent) override; private: QStringList fileList; int fileCount; ///fetchMore每次加载的最大数据行数 int PAGE_ITEM_COUNT; }; //![0] #endif // FILELISTMODEL_H filelistmodel.cpp #include "filelistmodel.h" #include #include #include #include FileListModel::FileListModel(QObject *parent) : QAbstractListModel(parent) ,PAGE_ITEM_COUNT(5) { } //![4] int FileListModel::rowCount(const QModelIndex & /* parent */) const { return fileCount; } QVariant FileListModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= fileList.size() || index.row() int batch = (index.row() / 100) % 2; if (batch == 0) return qApp->palette().base(); else return qApp->palette().alternateBase(); } return QVariant(); } //![4] //![1] bool FileListModel::canFetchMore(const QModelIndex & /* index */) const { if (fileCount PAGE_ITEM_COUNT = value; } //![2] //![0] void FileListModel::setDirPath(const QString &path) { QDir dir(path); beginResetModel(); fileList = dir.entryList(); fileCount = 0; endResetModel(); } //![0] 使用 FileListModel *model = new FileListModel(this); model->setDirPath(QLibraryInfo::location(QLibraryInfo::PrefixPath)); //一次fetchMore最大加载数量 QLabel *lb = new QLabel(tr("Fetch Max Count:")); QSpinBox *sbMaxCount = new QSpinBox(); QLabel *label = new QLabel(tr("&Directory:")); QLineEdit *lineEdit = new QLineEdit; label->setBuddy(lineEdit); QListView *view = new QListView; view->setModel(model); logViewer = new QTextBrowser; logViewer->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred)); connect(sbMaxCount, QOverload::of(&QSpinBox::valueChanged), model, &FileListModel::setPAGE_ITEM_COUNT); sbMaxCount->setValue(3); connect(lineEdit, &QLineEdit::textChanged, model, &FileListModel::setDirPath); connect(lineEdit, &QLineEdit::textChanged, logViewer, &QTextEdit::clear); connect(model, &FileListModel::numberPopulated, this, [&](int number) { logViewer->append(tr("%1 items added.").arg(number)); }); QGridLayout *layout = new QGridLayout; layout->addWidget(lb,0,0); layout->addWidget(sbMaxCount,0,1); layout->addWidget(label, 1, 0); layout->addWidget(lineEdit, 1, 1); layout->addWidget(view, 2, 0, 1, 2); layout->addWidget(logViewer, 3, 0, 1, 2); setLayout(layout); setWindowTitle(tr("Fetch More Example")); 说明

当滑动滚动条,模型还没完全加载数据,继续加载显示 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3