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¶
Base classes¶
Subclasses¶
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
parentobject.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:
IntEnumCustom 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.mPathAvailable as
QgsBrowserModel.PathRolein older QGIS releases.Comment: Item commentAvailable as
QgsBrowserModel.CommentRolein older QGIS releases.Sort: Custom sort role, see QgsDataItem.sortKey()Available as
QgsBrowserModel.SortRolein older QGIS releases.ProviderKey: Data item provider key that created the item, see QgsDataItem.providerKey()Added in version 3.12.
Available as
QgsBrowserModel.ProviderKeyRolein older QGIS releases.LayerMetadata:Available as
QgsBrowserModel.LayerMetadataRolein older QGIS releases.
- ItemDataRole¶
alias of
CustomRole
- addFavoriteDirectory(self, directory: str | None, name: str | None = '')[source]¶
Adds a
directoryto the favorites group.If
nameis specified, it will be used as the favorite’s name. Otherwise the name will be set to matchdirectory.See also
- Parameters:
directory (Optional[str])
name (Optional[str] = '')
- 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
Noneif 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:
- 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
parentitem is argument is specified, then only items which are children ofparentare searched. If noparentis specified, then all items within the model are searched.- Parameters:
item (Optional[QgsDataItem])
parent (Optional[QgsDataItem] = None)
- 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(self) bool[source]¶
Returns
Trueif the model has been initialized.See also
- 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:
item (Optional[QgsDataItem])
oldState (Qgis.BrowserItemState)
- 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.
- removeFavorite(self, index: QModelIndex)[source]¶
Removes a favorite directory from its corresponding model index.
See also
- Parameters:
index (QModelIndex)
- rootItems(self) List[QgsDataItem]¶
Returns the root items for the model.
Added in version 3.28.
- Return type:
- 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.