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);
}
};
非常感謝! –