19 : QAbstractTableModel( parent )
20 , mQueryResult( queryResult )
21 , mColumns( queryResult.columns() )
23 qRegisterMetaType< QList<QList<QVariant>>>(
"QList<QList<QVariant>>" );
26 mWorker =
new QgsQueryResultFetcher( &mQueryResult );
27 mWorker->moveToThread( &mWorkerThread );
28 connect( &mWorkerThread, &QThread::started, mWorker, &QgsQueryResultFetcher::fetchRows );
30 mWorkerThread.start();
36 beginInsertRows( QModelIndex(), mRows.count( ), mRows.count( ) + rows.count() - 1 );
45 mWorker->stopFetching();
53 mWorker->stopFetching();
56 mWorker->deleteLater();
62 if ( parent.isValid() )
69 if ( parent.isValid() )
71 return mColumns.count();
76 if ( !index.isValid() || index.row() < 0 || index.column() >= mColumns.count() ||
77 index.row() >= mRows.count( ) )
84 const QList<QVariant> result = mRows.at( index.row() );
85 if ( index.column() < result.count( ) )
87 return result.at( index.column() );
97 if ( orientation == Qt::Orientation::Horizontal && role == Qt::ItemDataRole::DisplayRole && section < mColumns.count() )
99 return mColumns.at( section );
101 return QAbstractTableModel::headerData( section, orientation, role );
106 const int QgsQueryResultFetcher::ROWS_TO_FETCH = 200;
108 void QgsQueryResultFetcher::fetchRows()
110 qlonglong rowCount { 0 };
111 QList<QList<QVariant>> newRows;
112 while ( mStopFetching == 0 && mQueryResult->hasNextRow() )
114 newRows.append( mQueryResult->nextRow() );
116 if ( rowCount % ROWS_TO_FETCH == 0 && mStopFetching == 0 )
118 emit rowsReady( newRows );
123 if ( rowCount % ROWS_TO_FETCH && mStopFetching == 0 )
125 emit rowsReady( newRows );
129 void QgsQueryResultFetcher::stopFetching()
int rowCount(const QModelIndex &parent) const override
QgsQueryResultModel(const QgsAbstractDatabaseProviderConnection::QueryResult &queryResult, QObject *parent=nullptr)
Constructs a QgsQueryResultModel from a queryResult with optional parent.
void rowsReady(const QList< QList< QVariant > > &rows)
Triggered when newRows have been fetched and can be added to the model.
QVariant data(const QModelIndex &index, int role) const override
int columnCount(const QModelIndex &parent) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
void cancel()
Cancels the row fetching.
The QueryResult class represents the result of a query executed by execSql()
bool hasNextRow() const
Returns true if there are more rows to fetch.