Class: QgsBrowserModel

A model for showing available data sources and other items in a structured tree.

QgsBrowserModel is the foundation for the QGIS browser panel, and includes items for the different data providers and folders accessible to users.

QgsBrowserModel models are not initially populated and use a deferred initialization approach. After constructing a QgsBrowserModel, a call must be made to initialize() in order to populate the model.

Note

Since QGIS 3.10 it is recommended to use QgsBrowserGuiModel from GUI library. Implementation of data items used from QgsBrowserModel should not trigger any GUI operations such as opening of widgets/dialogs or showing message boxes. Such actions should be implemented in a new QgsDataItemGuiProvider subclass which is used by QgsBrowserGuiModel (but not by QgsBrowserModel).

Class Hierarchy

Inheritance diagram of qgis.core.QgsBrowserModel

Base classes

QAbstractItemModel

QObject

Subclasses

QgsBrowserGuiModel

A model for showing available data sources and other items in a structured tree.

class qgis.core.QgsBrowserModel[source]

Bases: QAbstractItemModel

__init__(parent: QObject | None = None)

Constructor for QgsBrowserModel, with the specified parent object.

Note

QgsBrowserModel models are not initially populated and use a deferred initialization approach. After constructing a QgsBrowserModel, a call must be made to initialize() in order to populate the model.

Parameters:

parent (Optional[QObject] = None)

class CustomRole(*values)

Bases: IntEnum

Custom model roles.

Note

Prior to QGIS 3.36 this was available as QgsBrowserModel.ItemDataRole

Added in version 3.36.

  • Path: Item path used to access path in the tree, see QgsDataItem.mPath

    Available as QgsBrowserModel.PathRole in older QGIS releases.

  • Comment: Item comment

    Available as QgsBrowserModel.CommentRole in older QGIS releases.

  • Sort: Custom sort role, see QgsDataItem.sortKey()

    Available as QgsBrowserModel.SortRole in older QGIS releases.

  • ProviderKey: Data item provider key that created the item, see QgsDataItem.providerKey()

    Added in version 3.12.

    Available as QgsBrowserModel.ProviderKeyRole in older QGIS releases.

  • LayerMetadata:

    Available as QgsBrowserModel.LayerMetadataRole in older QGIS releases.

ItemDataRole

alias of CustomRole

addFavoriteDirectory(self, directory: str | None, name: str | None = '')[source]

Adds a directory to the favorites group.

If name is specified, it will be used as the favorite’s name. Otherwise the name will be set to match directory.

See also

removeFavorite()

Parameters:
  • directory (Optional[str])

  • name (Optional[str] = '')

addRootItems(self)[source]

Populates the model

beginInsertItems(self, parent: QgsDataItem | None, first: int, last: int)[source]
Parameters:
  • parent (Optional[QgsDataItem])

  • first (int)

  • last (int)

beginRemoveItems(self, parent: QgsDataItem | None, first: int, last: int)[source]
Parameters:
  • parent (Optional[QgsDataItem])

  • first (int)

  • last (int)

connectItem(self, item: QgsDataItem | None)[source]

Deprecated since version 3.4.

This method has no effect, and is dangerous to call in earlier QGIS versions. Any usage should be removed (and will have no harmful side-effects!).

Parameters:

item (Optional[QgsDataItem])

signal connectionsChanged[source]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

dataItem(self, idx: QModelIndex) QgsDataItem | None[source]

Returns the data item at the specified index, or None if no item exists at the index.

Parameters:

idx (QModelIndex)

Return type:

Optional[QgsDataItem]

driveItems(self) Any

Returns a map of the root drive items shown in the browser.

These correspond to the top-level directory items shown, e.g. on Windows the C:, D:, etc, and on Linux the “/” root directory.

Added in version 3.6.

Return type:

Any

endInsertItems(self)[source]
endRemoveItems(self)[source]
findItem(self, item: QgsDataItem | None, parent: QgsDataItem | None = None) QModelIndex[source]

Returns the model index corresponding to the specified data item. If the item was not found, an invalid QModelIndex is returned.

If the parent item is argument is specified, then only items which are children of parent are searched. If no parent is specified, then all items within the model are searched.

Parameters:
Return type:

QModelIndex

findPath(self, path: str | None, matchFlag: Qt.MatchFlag = Qt.MatchExactly) QModelIndex[source]

Returns index of item with given path. It only searches in currently fetched items, i.e. it does not fetch children.

Parameters:
  • path (Optional[str]) – item path

  • matchFlag (Qt.MatchFlag = Qt.MatchExactly) – supported is Qt.MatchExactly and Qt.MatchStartsWith which has reverse meaning, i.e. find item with the longest match from start with path (to get as close/deep as possible to deleted item).

Return type:

QModelIndex

Returns:

model index, invalid if item not found

findUri(self, uri: str | None, index: QModelIndex = QModelIndex()) QModelIndex[source]

Returns index of layer item with given uri. It only searches in currently fetched items, i.e. it does not fetch children.

Parameters:
  • uri (Optional[str]) – item uri

  • index (QModelIndex = QModelIndex()) – the current index of the parent (to search for children)

Return type:

QModelIndex

Returns:

model index, invalid if item not found

Added in version 3.6.

hidePath(self, item: QgsDataItem | None)[source]

Hide the given path in the browser model

Parameters:

item (Optional[QgsDataItem])

initialize(self)[source]

Delayed initialization, needed because the provider registry must be already populated.

See also

initialized()

initialized(self) bool[source]

Returns True if the model has been initialized.

See also

initialize()

Return type:

bool

itemDataChanged(self, item: QgsDataItem | None)[source]
Parameters:

item (Optional[QgsDataItem])

itemStateChanged(self, item: QgsDataItem | None, oldState: Qgis.BrowserItemState)[source]

Emitted when an item’s state is changed.

Parameters:
refresh(self, path: str | None)[source]

Refresh item specified by path

refresh(self, index: QModelIndex = QModelIndex()) Refresh item children

Parameters:

path (Optional[str])

refreshDrives(self)[source]

Refreshes the list of drive items, removing any corresponding to removed drives and adding newly added drives.

Added in version 3.4.

reload(self)[source]

Reload the whole model

removeFavorite(self, index: QModelIndex)[source]

Removes a favorite directory from its corresponding model index.

Parameters:

index (QModelIndex)

removeRootItems(self)[source]
rootItems(self) List[QgsDataItem]

Returns the root items for the model.

Added in version 3.28.

Return type:

List[QgsDataItem]

signal stateChanged[source]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

updateProjectHome(self)[source]