Class: QgsQueryBuilder

Query Builder for layers.

The query builder allows interactive creation of a SQL for limiting the features displayed in a vector layer. The fields in the table are displayed and sample values (or all values) can be viewed to aid in constructing the query. A test function returns the number of features that will be returned.

Class Hierarchy

Inheritance diagram of qgis.gui.QgsQueryBuilder

Base classes

QgsSubsetStringEditorInterface

Interface for a dialog that can edit subset strings.

QDialog

QWidget

QObject

QPaintDevice

Methods

clear

codeEditorWidget

Returns the code editor widget for the SQL.

loadQuery

Load query from the XML file

saveQuery

Save query to the XML file

setDatasourceDescription

setSql

Set the sql statement to display in the dialog.

sql

Returns the sql statement entered in the dialog.

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsQueryBuilder. See the FAQ for more details.

test

The default implementation tests that the constructed sql statement to see if the vector layer data provider likes it.

Static Methods

loadQueryFromFile

Load query from the XML file

saveQueryToFile

Save query to the XML file

class qgis.gui.QgsQueryBuilder[source]

Bases: QgsSubsetStringEditorInterface

__init__(layer: QgsVectorLayer | None, parent: QWidget | None = None, fl: Qt.WindowFlags | Qt.WindowType = QgsGuiUtils.ModalDialogFlags)

This constructor is used when the query builder is called from the vector layer properties dialog

Parameters:
  • layer (Optional[QgsVectorLayer]) – existing vector layer

  • parent (Optional[QWidget] = None) – Parent widget

  • fl (Union[Qt.WindowFlags, Qt.WindowType] = QgsGuiUtils.ModalDialogFlags) – dialog flags

clear(self)[source]
codeEditorWidget(self) QgsCodeEditor | None[source]

Returns the code editor widget for the SQL.

Added in version 3.18.

Return type:

Optional[QgsCodeEditor]

loadQuery(self)[source]

Load query from the XML file

Added in version 3.16.

static loadQueryFromFile(subset: str | None) bool[source]

Load query from the XML file

Added in version 3.26.

Parameters:

subset (Optional[str])

Return type:

bool

saveQuery(self)[source]

Save query to the XML file

Added in version 3.16.

static saveQueryToFile(subset: str | None) bool[source]

Save query to the XML file

Added in version 3.26.

Parameters:

subset (Optional[str])

Return type:

bool

setDatasourceDescription(self, uri: str | None)[source]
Parameters:

uri (Optional[str])

setSql(self, sqlStatement: str | None)[source]

Set the sql statement to display in the dialog.

Parameters:

sqlStatement (Optional[str])

sql(self) str[source]

Returns the sql statement entered in the dialog.

Return type:

str

virtual test(self)[source]

The default implementation tests that the constructed sql statement to see if the vector layer data provider likes it. The number of rows that would be returned is displayed in a message box. The test uses a “select

QgsQueryBuilder.test(*) from ..." query to test the SQL statement.

test(*) from …” query to test the SQL statement.