首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从qt模型中获取所有数据

如何从qt模型中获取所有数据
EN

Stack Overflow用户
提问于 2016-08-26 21:38:43
回答 1查看 399关注 0票数 1

我已经创建了一个名为proxymodel的QIdentityProxyModel,它通过添加3个计算列扩展了一个名为QSqlTableModel的源模型。计算列是通过遍历源模型并将数据存储在由代理模型映射的列表中来生成的。代理模型显示在TableView中。

我遇到的问题是,除非我与TableView交互,否则模型只能加载总共5426个寄存器的前256个寄存器,所以最初我只能在这前256行上执行计算。

我希望在列表中填入5426行的计算结果。请!帮我搞定这件事?任何想法都会有帮助

这个项目是用pyqt写的,所以你想怎么回答都可以!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-27 01:09:03

SQL模型使用渐进式获取。源模型从canFetchMore返回true。视图调用fetchMore,然后通过从数据库获取更多行来将更多行添加到源模型中-只有在视图需要它们的时候。

由于您的代理需要所有数据,因此它应该在空闲时间(使用零持续时间计时器)调用源模型上的fetchMore。它还应该正确地跟踪源,并向其插入更多的行!

代码语言:javascript
复制
class MyProxy : public QIdentityProxyModel {
  Q_OBJECT
  QMetaObject::Connection m_onRowsInserted;
  ...
  /// Update the computed results based on data in rows first through last
  /// in the source model.
  void calculate(int first, int last);
  void onRowsInserted(const QModelIndex & parent, int first, int last) {
    calculate(int first, int last);
  }
  void onSourceModelChanged() {
    disconnect(m_onRowsInserted);
    m_onRowsInserted = connect(sourceModel(), &QAbstractItemModel::rowsInserted,
                               this, &MyProxy::onRowsInserted);
    fetch();
  }
  void fetch() {
    if (!sourceModel()->canFetchMore(QModelIndex{})) return;
    QTimer::singleShot(0, this, [this]{
      if (!sourceModel()->canFetchMore(QModelIndex{})) return;
      sourceModel()->fetchMore(QModelIndex{});
      fetch();
    });
  }
public:
  MyProxy(QObject * parent = nullptr) : QIdentityProxyModel{parent} {
    connect(this, &QAbstractProxyModel::sourceModelChanged,
            this, &MyProxy::onSourceModelChanged);
  }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39167722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档