【问题标题】:How to get all data from qt model如何从qt模型中获取所有数据
【发布时间】:2016-08-26 13:38:43
【问题描述】:

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

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

我希望用 5426 行的计算来填充列表。 请!帮我完成这个?任何想法都会有帮助

项目是用pyqt编写的,所以你可以随意回答!

【问题讨论】:

    标签: python c++ qt model


    【解决方案1】:

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

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

    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);
      }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 2021-05-02
      • 1970-01-01
      • 2022-11-27
      • 2021-07-01
      • 1970-01-01
      • 2017-04-21
      相关资源
      最近更新 更多