Fetch More Example
2021-04-08 11:48 清晨、午后 阅读(121) 评论(0) 编辑 收藏 举报
TestModel.h
class TestModel : public QAbstractItemModel { Q_OBJECT public: enum Columns {Number, Group, Sn, DeviceSN, AuthObject, AuthProduct, RecoveryTime, ColumnSize}; TestModel(QObject *parent = nullptr) { m_headers << tr("No.") << tr("Group") << tr("Serial Number") << tr("Device SN") << tr("Authorized User") << tr("Product") << tr("Time Restored"); } virtual QModelIndex index(int row, int column, const QModelIndex &parent) const Q_DECL_OVERRIDE; virtual QModelIndex parent(const QModelIndex &child) const Q_DECL_OVERRIDE; int rowCount(const QModelIndex &parent = QModelIndex()) const override; virtual int columnCount(const QModelIndex &) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; signals: void numberPopulated(int number); public slots: void setData(const QList<Data::ActiveInfoPtr> &infosPtr); protected: bool canFetchMore(const QModelIndex &parent) const override; void fetchMore(const QModelIndex &parent) override; private: QList<Data::ActiveInfoPtr> m_infosPtr; int fileCount; QStringList m_headers; };
TestModel.cpp
void TestModel::setData(const QList<Data::ActiveInfoPtr> &infosPtr) { beginResetModel(); m_infosPtr = infosPtr; fileCount = 0; endResetModel(); } bool TestModel::canFetchMore(const QModelIndex &parent) const { if (parent.isValid()) return false; return (fileCount < m_infosPtr.size()); } void TestModel::fetchMore(const QModelIndex &parent) { if (parent.isValid()) return; int remainder = m_infosPtr.size() - fileCount; int itemsToFetch = qMin(100, remainder); if (itemsToFetch <= 0) return; beginInsertRows(QModelIndex(), fileCount, fileCount + itemsToFetch - 1); fileCount += itemsToFetch; endInsertRows(); emit numberPopulated(itemsToFetch); } QModelIndex TestModel::index(int row, int column, const QModelIndex &parent) const { Q_UNUSED(parent) if (m_infosPtr.empty()) return QModelIndex(); if (!m_infosPtr.empty()) { ActiveInfoPtr info = m_infosPtr.at(static_cast<size_t>(row)); if (info.isNull()) return QModelIndex(); return createIndex(row, column, info.data()); } return QModelIndex(); } QModelIndex TestModel::parent(const QModelIndex &child) const { return QModelIndex(); } int TestModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : fileCount; } int TestModel::columnCount(const QModelIndex &) const { return Columns::ColumnSize; } QVariant TestModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); int row = index.row(); int column = index.column(); if (row >= m_infosPtr.size()) return QVariant(); switch (role) { case Qt::DisplayRole: if (column == Columns::Number) return m_infosPtr.at(row)->number(); if (column == Columns::Group) return m_infosPtr.at(row)->group(); if (column == Columns::Sn) return Utils::generateXXX(m_infosPtr.at(row)->sn()); if (column == Columns::DeviceSN) return m_infosPtr.at(row)->deviceSn(); if (column == Columns::AuthProduct) return Utils::authObject(m_infosPtr.at(row)->authProduct()); if (column == Columns::AuthObject) return m_infosPtr.at(row)->authObject(); if (column == Columns::RecoveryTime) return Utils::timeStampFormat(m_infosPtr.at(row)->recoveryTime()); return QVariant(); default: return QVariant(); } } QVariant TestModel::headerData(int section, Qt::Orientation orientation, int role) const { if (Qt::Vertical == orientation) return QVariant(); if (role == Qt::DisplayRole) { if (section >= m_headers.size()) return QVariant(); return m_headers.at(section); } return QVariant(); }
widget 中使用
m_listModel = new TestModel(this); m_view->setModel(m_listModel); m_listModel->setData(infosPtr);
-----
setData
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架