Class: QgsDatabaseTableModel

A model containing tables from a database connection.

This class does not automatically subscribe to database updates. Tables are queried from the database initially upon model construction. In order to update the listed tbales, QgsDatabaseTableModel.refresh() must be manually called.

Added in version 3.14.

Class Hierarchy

Inheritance diagram of qgis.core.QgsDatabaseTableModel

Base classes

QAbstractItemModel

QObject

Enums

CustomRole

Custom model roles.

Role

alias of CustomRole

Methods

allowEmptyTable

Returns True if the model allows the empty table ("not set") choice.

refresh

Refreshes the table list by querying the underlying connection.

setAllowEmptyTable

Sets whether an optional empty table ("not set") option is present in the model.

class qgis.core.QgsDatabaseTableModel[source]

Bases: QAbstractItemModel

__init__(provider: str | None, connection: str | None, schema: str | None = '', parent: QObject | None = None)

Constructor for QgsDatabaseTableModel, for the specified provider and connection name.

The optional schema argument can be used to restrict the tables to those from a specific schema.

Warning

The provider must support the connection API methods in its QgsProviderMetadata implementation in order for the model to work correctly.

Parameters:
  • provider (Optional[str])

  • connection (Optional[str])

  • schema (Optional[str] = '')

  • parent (Optional[QObject] = None)

__init__(connection: QgsAbstractDatabaseProviderConnection | None, schema: str | None = '', parent: QObject | None = None)

Constructor for QgsDatabaseTableModel, for the specified connection.

The optional schema argument can be used to restrict the tables to those from a specific schema.

Ownership of connection is transferred to the model.

Parameters:
class CustomRole(*values)

Bases: IntEnum

Custom model roles.

Note

Prior to QGIS 3.36 this was available as QgsDatabaseTableModel.Role

Added in version 3.36.

  • TableName: Table name

    Available as QgsDatabaseTableModel.RoleTableName in older QGIS releases.

  • Schema: Table schema

    Available as QgsDatabaseTableModel.RoleSchema in older QGIS releases.

  • TableFlags: Table flags role

    Available as QgsDatabaseTableModel.RoleTableFlags in older QGIS releases.

  • Comment: Comment role

    Available as QgsDatabaseTableModel.RoleComment in older QGIS releases.

  • CustomInfo: Custom info variant map role

    Available as QgsDatabaseTableModel.RoleCustomInfo in older QGIS releases.

  • WkbType: WKB type for primary (first) geometry column in table

    Available as QgsDatabaseTableModel.RoleWkbType in older QGIS releases.

  • Crs: CRS for primary (first) geometry column in table

    Available as QgsDatabaseTableModel.RoleCrs in older QGIS releases.

  • Empty: Entry is an empty entry

    Available as QgsDatabaseTableModel.RoleEmpty in older QGIS releases.

Role

alias of CustomRole

allowEmptyTable(self) bool[source]

Returns True if the model allows the empty table (“not set”) choice.

Return type:

bool

refresh(self)[source]

Refreshes the table list by querying the underlying connection.

setAllowEmptyTable(self, allowEmpty: bool)[source]

Sets whether an optional empty table (“not set”) option is present in the model.

Parameters:

allowEmpty (bool)