Class: QgsQueryResultWidget

The QgsQueryResultWidget class allows users to enter and run an SQL query on a DB connection (an instance of QgsAbstractDatabaseProviderConnection).

Query results are displayed in a table view. Query execution and result fetching can be interrupted by pressing the “Stop” push button.

The widget supports a few QueryWidgetMode modes that pre-configure the widget appearance to be used in different contexts like when updating the SQL of an existing query layer.

Note

the ownership of the connection is transferred to the widget.

Added in version 3.22.

Class Hierarchy

Inheritance diagram of qgis.gui.QgsQueryResultWidget

Base classes

QWidget

QObject

QPaintDevice

Enums

QueryWidgetMode

The QueryWidgetMode enum represents various modes for the widget appearance.

Methods

copyResults

Copies the query results to the clipboard, as a formatted table.

executeQuery

Starts executing the query.

notify

Displays a message with text title and level in the widget's message bar.

setConnection

Sets the connection to connection, ownership is transferred to the widget.

setQuery

Convenience method to set the SQL editor text to sql.

setSqlVectorLayerOptions

Initializes the widget from options.

setWidgetMode

Sets the widget mode to widgetMode, default is SqlQueryMode.

showError

Hides the result table and shows the error title and message in the message bar or in the SQL error panel is isSqlError is set.

tokensReady

Triggered when the threaded API fetcher has new tokens to add.

Signals

createSqlVectorLayer

Emitted when a new vector SQL (query) layer must be created.

firstResultBatchFetched

Emitted when the first batch of results has been fetched.

class qgis.gui.QgsQueryResultWidget[source]

Bases: QWidget

__init__(parent: QWidget | None = None, connection: QgsAbstractDatabaseProviderConnection | None = None)

Creates a QgsQueryResultWidget with the given connection, ownership is transferred to the widget.

Parameters:
class QueryWidgetMode(*values)

Bases: IntEnum

The QueryWidgetMode enum represents various modes for the widget appearance.

  • SqlQueryMode: Defaults widget mode for SQL execution and SQL query layer creation.

  • QueryLayerUpdateMode: SQL query layer update mode: the create SQL layer button is renamed to ‘Update’ and the SQL layer creation group box is expanded.

QueryLayerUpdateMode = 2
SqlQueryMode = 1
copyResults(self)[source]

Copies the query results to the clipboard, as a formatted table.

Added in version 3.32.

copyResults(self, fromRow: int, toRow: int, fromColumn: int, toColumn: int)[source]

Copies a range of the query results to the clipboard, as a formatted table.

Added in version 3.32.

Parameters:
  • fromRow (int)

  • toRow (int)

  • fromColumn (int)

  • toColumn (int)

signal createSqlVectorLayer(providerKey: str, connectionUri: str, options: QgsAbstractDatabaseProviderConnection.SqlVectorLayerOptions)[source]

Emitted when a new vector SQL (query) layer must be created.

Parameters:
executeQuery(self)[source]

Starts executing the query.

signal firstResultBatchFetched[source]

Emitted when the first batch of results has been fetched.

Note

If the query returns no results this signal is not emitted.

notify(self, title: str | None, text: str | None, level: Qgis.MessageLevel = Qgis.MessageLevel.Info)[source]

Displays a message with text title and level in the widget’s message bar.

Parameters:
  • title (Optional[str])

  • text (Optional[str])

  • level (Qgis.MessageLevel = Qgis.MessageLevel.Info)

setConnection(self, connection: QgsAbstractDatabaseProviderConnection | None)[source]

Sets the connection to connection, ownership is transferred to the widget.

Parameters:

connection (Optional[QgsAbstractDatabaseProviderConnection])

setQuery(self, sql: str | None)[source]

Convenience method to set the SQL editor text to sql.

Parameters:

sql (Optional[str])

setSqlVectorLayerOptions(self, options: QgsAbstractDatabaseProviderConnection.SqlVectorLayerOptions)[source]

Initializes the widget from options.

Parameters:

options (QgsAbstractDatabaseProviderConnection.SqlVectorLayerOptions)

setWidgetMode(self, widgetMode: QgsQueryResultWidget.QueryWidgetMode)[source]

Sets the widget mode to widgetMode, default is SqlQueryMode.

Parameters:

widgetMode (QgsQueryResultWidget.QueryWidgetMode)

showError(self, title: str | None, message: str | None, isSqlError: bool = False)[source]

Hides the result table and shows the error title and message in the message bar or in the SQL error panel is isSqlError is set.

Parameters:
  • title (Optional[str])

  • message (Optional[str])

  • isSqlError (bool = False)

tokensReady(self, tokens: Iterable[str | None])[source]

Triggered when the threaded API fetcher has new tokens to add.

Parameters:

tokens (Iterable[Optional[str]])