Class: QgsBrowserModel

class qgis.core.QgsBrowserModel

Bases: PyQt5.QtCore.QAbstractItemModel

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).

QgsBrowserModel(parent: QObject = 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.

Methods

addFavoriteDirectory

Adds a directory to the favorites group.

addRootItems

Populates the model

beginInsertColumns

beginInsertItems

param parent:

beginInsertRows

beginMoveColumns

beginMoveRows

beginRemoveColumns

beginRemoveItems

param parent:

beginRemoveRows

beginResetModel

canFetchMore

param parent:

changePersistentIndex

changePersistentIndexList

childEvent

columnCount

param parent:

connectItem

connectNotify

createIndex

customEvent

data

param index:

dataItem

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

decodeData

disconnectNotify

driveItems

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

dropMimeData

param data:

encodeData

endInsertColumns

endInsertItems

endInsertRows

endMoveColumns

endMoveRows

endRemoveColumns

endRemoveItems

endRemoveRows

endResetModel

fetchMore

param parent:

findItem

Returns the model index corresponding to the specified data item.

findPath

Returns index of item with given path.

findUri

Returns index of layer item with given uri.

flags

param index:

hasChildren

param parent:

headerData

param section:

hidePath

Hide the given path in the browser model

index

param row:

initialize

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

initialized

Returns True if the model has been initialized.

isSignalConnected

itemDataChanged

param item:

itemStateChanged

Emitted when an item's state is changed.

mimeData

param indexes:

mimeTypes

rtype:

List[str]

parent

param index:

persistentIndexList

receivers

refresh

Refresh item specified by path

refreshDrives

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

reload

Reload the whole model

removeFavorite

Removes a favorite directory from its corresponding model index.

removeRootItems

resetInternalData

rootItems

Returns the root items for the model.

rowCount

param parent:

sender

senderSignalIndex

setData

param index:

timerEvent

updateProjectHome

Signals

connectionsChanged

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

stateChanged

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

Attributes

CommentRole

LayerMetadataRole

PathRole

ProviderKeyRole

SortRole

CommentRole = 257
class ItemDataRole

Bases: int

LayerMetadataRole = 260
PathRole = 256
ProviderKeyRole = 259
SortRole = 258
addFavoriteDirectory(self, directory: str, name: str = '')

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()

New in version 3.0.

Parameters:
  • directory (str) –

  • name (str = '') –

addRootItems(self)

Populates the model

beginInsertColumns(self, QModelIndex, int, int)
beginInsertItems(self, parent: QgsDataItem, first: int, last: int)
Parameters:
  • parent (QgsDataItem) –

  • first (int) –

  • last (int) –

beginInsertRows(self, QModelIndex, int, int)
beginMoveColumns(self, QModelIndex, int, int, QModelIndex, int) bool
beginMoveRows(self, QModelIndex, int, int, QModelIndex, int) bool
beginRemoveColumns(self, QModelIndex, int, int)
beginRemoveItems(self, parent: QgsDataItem, first: int, last: int)
Parameters:
  • parent (QgsDataItem) –

  • first (int) –

  • last (int) –

beginRemoveRows(self, QModelIndex, int, int)
beginResetModel(self)
canFetchMore(self, parent: QModelIndex) bool
Parameters:

parent (QModelIndex) –

Return type:

bool

changePersistentIndex(self, QModelIndex, QModelIndex)
changePersistentIndexList(self, Iterable[QModelIndex], Iterable[QModelIndex])
childEvent(self, QChildEvent)
columnCount(self, parent: QModelIndex = QModelIndex()) int
Parameters:

parent (QModelIndex = QModelIndex()) –

Return type:

int

connectItem(self, item: QgsDataItem)

Deprecated since version Deprecated: since QGIS 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 (QgsDataItem) –

connectNotify(self, QMetaMethod)
connectionsChanged

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.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

createIndex(self, int, int, object: object = 0) QModelIndex
customEvent(self, QEvent)
data(self, index: QModelIndex, role: int = Qt.DisplayRole) Any
Parameters:
  • index (QModelIndex) –

  • role (int = Qt.DisplayRole) –

Return type:

Any

dataItem(self, idx: QModelIndex) QgsDataItem

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

Parameters:

idx (QModelIndex) –

Return type:

QgsDataItem

decodeData(self, int, int, QModelIndex, QDataStream) bool
disconnectNotify(self, QMetaMethod)
driveItems(self) object

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.

New in version 3.6.

Return type:

object

dropMimeData(self, data: QMimeData, action: Qt.DropAction, row: int, column: int, parent: QModelIndex) bool
Parameters:
  • data (QMimeData) –

  • action (Qt.DropAction) –

  • row (int) –

  • column (int) –

  • parent (QModelIndex) –

Return type:

bool

encodeData(self, Iterable[QModelIndex], QDataStream)
endInsertColumns(self)
endInsertItems(self)
endInsertRows(self)
endMoveColumns(self)
endMoveRows(self)
endRemoveColumns(self)
endRemoveItems(self)
endRemoveRows(self)
endResetModel(self)
fetchMore(self, parent: QModelIndex)
Parameters:

parent (QModelIndex) –

findItem(self, item: QgsDataItem, parent: QgsDataItem = None) QModelIndex

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, matchFlag: Qt.MatchFlag = Qt.MatchExactly) QModelIndex

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

Parameters:
  • path (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, index: QModelIndex = QModelIndex()) QModelIndex

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

Parameters:
  • uri (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

New in version 3.6.

flags(self, index: QModelIndex) Qt.ItemFlags
Parameters:

index (QModelIndex) –

Return type:

Qt.ItemFlags

hasChildren(self, parent: QModelIndex = QModelIndex()) bool
Parameters:

parent (QModelIndex = QModelIndex()) –

Return type:

bool

headerData(self, section: int, orientation: Qt.Orientation, role: int = Qt.DisplayRole) Any
Parameters:
  • section (int) –

  • orientation (Qt.Orientation) –

  • role (int = Qt.DisplayRole) –

Return type:

Any

hidePath(self, item: QgsDataItem)

Hide the given path in the browser model

Parameters:

item (QgsDataItem) –

index(self, row: int, column: int, parent: QModelIndex = QModelIndex()) QModelIndex
Parameters:
  • row (int) –

  • column (int) –

  • parent (QModelIndex = QModelIndex()) –

Return type:

QModelIndex

initialize(self)

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

See also

initialized()

initialized(self) bool

Returns True if the model has been initialized.

See also

initialize()

Return type:

bool

isSignalConnected(self, QMetaMethod) bool
itemDataChanged(self, item: QgsDataItem)
Parameters:

item (QgsDataItem) –

itemStateChanged(self, item: QgsDataItem, oldState: Qgis.BrowserItemState)

Emitted when an item’s state is changed.

Parameters:
mimeData(self, indexes: Iterable[QModelIndex]) QMimeData
Parameters:

indexes (Iterable[QModelIndex]) –

Return type:

QMimeData

mimeTypes(self) List[str]
Return type:

List[str]

parent(self, index: QModelIndex) QModelIndex
Parameters:

index (QModelIndex) –

Return type:

QModelIndex

persistentIndexList(self) List[QModelIndex]
receivers(self, PYQT_SIGNAL) int
refresh(self, path: str)

Refresh item specified by path

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

Parameters:

path (str) –

refreshDrives(self)

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

New in version 3.4.

reload(self)

Reload the whole model

removeFavorite(self, index: QModelIndex)

Removes a favorite directory from its corresponding model index.

New in version 3.0.

Parameters:

index (QModelIndex) –

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

Returns the root items for the model.

New in version 3.28.

Return type:

List[QgsDataItem]

rowCount(self, parent: QModelIndex = QModelIndex()) int
Parameters:

parent (QModelIndex = QModelIndex()) –

Return type:

int

sender(self) QObject
senderSignalIndex(self) int
setData(self, index: QModelIndex, value: Any, role: int = Qt.EditRole) bool
Parameters:
  • index (QModelIndex) –

  • value (Any) –

  • role (int = Qt.EditRole) –

Return type:

bool

stateChanged

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.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

timerEvent(self, QTimerEvent)
updateProjectHome(self)