我已经创建了一个名为proxymodel的QIdentityProxyModel,它通过添加3个计算列扩展了一个名为QSqlTableModel的源模型。计算列是通过遍历源模型并将数据存储在由代理模型映射的列表中来生成的。代理模型显示在TableView中。
我遇到的问题是,除非我与TableView交互,否则模型只能加载总共5426个寄存器的前256个寄存器,所以最初我只能在这前256行上执行计算。
我希望在列表中填入5426行的计算结果。请!帮我搞定这件事?任何想法都会有帮助
这个项目是用pyqt写的,所以你想怎么回答都可以!
发布于 2016-08-27 01:09:03
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);
}
};https://stackoverflow.com/questions/39167722
复制相似问题