2016-08-26 39 views
1

我創建了一個名爲proxymodel的QIdentityProxyModel,它通過添加3個計算列來擴展名爲sourcemodel的QSqlTableModel。 計算列是通過遍歷sourcemodel並將數據存儲在由代理模型映射的列表中進行的。 proxymodel顯示在TableView中。如何從qt模型獲取所有數據

我遇到的問題是,除非我與TableView交互模型只加載總共5426的前256個寄存器,所以最初我只能執行這些前256行的計算。

我希望填寫5426行上的計算列表。請致電 !幫我完成這件事?任何想法都會有所幫助

該項目是用pyqt編寫的,因此可以自由地回答你想要的東西!

回答

0

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

非常感謝! –

相關問題