当前位置: 首页 > news >正文

Qt文档阅读笔记-Fetch More Example解析

Fetch More Example这个例子说明了如何在视图模型上添加记录。

这个例子由一个对话框组成,在Directory的输入框中,可输入路径信息。应用程序会载入路径信息的文件信息等。不需要按回车键就能搜索。

当有大量数据时,需要对视图模型进行批量增加。

此案例,实现了FileListModel类,此类包含了一个视图模型,这个视图模型获取路径下的文件。

下面来看下FileListModel的代码。

FileListModel Class Definition

FileListModel继承了QAbstractListModel并且存储了路径信息。只有视图自己请求添加项时,才会进行添加。

 class FileListModel : public QAbstractListModel{Q_OBJECTpublic: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);protected:bool canFetchMore(const QModelIndex &parent) const override;void fetchMore(const QModelIndex &parent) override;private:QStringList fileList;int fileCount;};

比较关键的2个函数是fetchMore()和canFetchMore(),这两个函数都是从QAbstractItemModel中继承下来的。当需要新增模型时,这2个函数就会被触发。

setDirPath()函数设置了当前模型的工作目录。当需要批量设置模型时,就会发出numberPopulated()信号。

所有文件条目都放到fileList里面,fileCount统计条目的数量。

FileListModel Class Implementation

首先来看下setDirPath()。

 void FileListModel::setDirPath(const QString &path){QDir dir(path);beginResetModel();fileList = dir.entryList();fileCount = 0;endResetModel();}

使用QDir获取目录内容。当要从模型中移除所有数据时需要通知QAbstractItemModel。

 bool FileListModel::canFetchMore(const QModelIndex & /* index */) const{if (fileCount < fileList.size())return true;elsereturn false;}

当需要更多项时,canFetchMore()函数会被触发。当不需要新增时此函数返回true,否则返回false。fetchMore()函数如下:

 void FileListModel::fetchMore(const QModelIndex & /* index */){int remainder = fileList.size() - fileCount;int itemsToFetch = qMin(100, remainder);if (itemsToFetch <= 0)return;beginInsertRows(QModelIndex(), fileCount, fileCount+itemsToFetch-1);fileCount += itemsToFetch;endInsertRows();emit numberPopulated(itemsToFetch);}

首先获取每一项的数量。beginInsertRow()和endInsertRow()在QAbstractItemModel中插入新行时,必须要调用的,最后emit numberPopulated()用于更新界面。

最后是rowCount()和data()

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() < 0)return QVariant();if (role == Qt::DisplayRole) {return fileList.at(index.row());} else if (role == Qt::BackgroundRole) {int batch = (index.row() / 100) % 2;if (batch == 0)return qApp->palette().base();elsereturn qApp->palette().alternateBase();}return QVariant();}

rowCount()函数是已经添加了的新行,不是目录中的条目数。

data()函数,从fileList中返回适当的条目。使用不同的背景颜色来区分。

http://www.lryc.cn/news/229955.html

相关文章:

  • QtC++与QTableView详解
  • HG/T 6002-2022 氟树脂粉末涂料检测
  • 【java】idea可以连接但看不到database相关的files
  • 信驰达科技加入车联网联盟(CCC),推进数字钥匙发展与应用
  • p9 Eureka-搭建eureka服务
  • 阶段七-Day01-SpringMVC
  • Python---集合中的交集 、并集 | 与差集 - 特性
  • C++调用lua脚本,包括全局函数绑定、类绑定,十分钟快速掌握
  • 快乐数[简单]
  • Spring源码阅读-ClassPathXmlApplicationContext
  • 考研分享第2期 | 中央财经大学管理科学跨考北大软微金融科技406分经验分享
  • Linux安装java jdk配置环境 方便查询
  • 惊群效应之Nginx处理
  • SpringBoot整合Ldap--超详细方法讲解
  • 【工程实践】Docker使用记录
  • FreeSwitch安装视频
  • SpringBoot3+Vue3+Mysql+Element Plus完成数据库存储blob类型图片,前端渲染后端传来的base64类型图片
  • 攻略 | 参与Moonbeam Ignite Ecosystem Tour
  • 【python自动化】Playwright基础教程(七)Keyboard键盘
  • Java读取文件内容写入新文件
  • 学习samba
  • 【Ansible】Ansible的Ad-hoc命令执行流程
  • Postgresql 常用整理
  • 如何在Jupyter Lab中安装不同的Kernel
  • Java钩子函数的使用
  • C++跨DLL内存所有权问题探幽(一)DLL提供的全局单例模式
  • 短时间不点击云服务器,自动化断开连接,怎么设置长时间
  • typhonjs-escomplex 代码可读性 可维护度探索
  • 支持向量机基本原理,Libsvm工具箱详细介绍,基于支持向量机SVM的人脸朝向识别
  • 密码破解工具的编写