QGIS API Documentation  3.20.0-Odense (decaadbb31)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsqueryresultmodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsqueryresultmodel.h - QgsQueryResultModel
3 
4  ---------------------
5  begin : 24.12.2020
6  copyright : (C) 2020 by Alessandro Pasotti
7  email : elpaso@itopen.it
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QgsQueryResultModel_H
17 #define QgsQueryResultModel_H
18 
19 #include <QAbstractTableModel>
20 #include <QThread>
21 
22 #include "qgis_core.h"
23 #include "qgis_sip.h"
24 
26 
28 
29 #ifndef SIP_RUN
30 
34 class QgsQueryResultFetcher: public QObject
35 {
36  Q_OBJECT
37 
38  public:
39 
41  QgsQueryResultFetcher( const QgsAbstractDatabaseProviderConnection::QueryResult *queryResult )
42  : mQueryResult( queryResult )
43  {}
44 
46  void fetchRows();
47 
49  void stopFetching();
50 
51  signals:
52 
54  void rowsReady( const QList<QList<QVariant>> &newRows );
55 
56  private:
57 
58  const QgsAbstractDatabaseProviderConnection::QueryResult *mQueryResult = nullptr;
59  QAtomicInt mStopFetching = 0;
60  // Batch of rows to fetch before emitting rowsReady
61  static const int ROWS_TO_FETCH;
62 
63 };
64 
65 #endif
66 
68 
69 
76 class CORE_EXPORT QgsQueryResultModel : public QAbstractTableModel
77 {
78  Q_OBJECT
79  public:
80 
84  QgsQueryResultModel( const QgsAbstractDatabaseProviderConnection::QueryResult &queryResult, QObject *parent = nullptr );
85 
87 
88  // QAbstractItemModel interface
89  public:
90 
91  int rowCount( const QModelIndex &parent ) const override;
92  int columnCount( const QModelIndex &parent ) const override;
93  QVariant data( const QModelIndex &index, int role ) const override;
94  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
95 
96  public slots:
97 
101  void rowsReady( const QList<QList<QVariant> > &rows );
102 
106  void cancel();
107 
108  private:
109 
111  QStringList mColumns;
112  QThread mWorkerThread;
113  QgsQueryResultFetcher *mWorker = nullptr;
114  QList<QVariantList> mRows;
115 
116 };
117 
118 #endif // qgsqueryresultmodel.h
The QgsQueryResultModel class is a model for QgsAbstractDatabaseProviderConnection::QueryResult.
The QueryResult class represents the result of a query executed by execSql()