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. |
Enums
Custom model roles. |
|
alias of |
Methods
Adds a directory to the favorites group. |
|
Populates the model |
|
Returns the data item at the specified index, or |
|
Returns a map of the root drive items shown in the browser. |
|
Returns the model index corresponding to the specified data item. |
|
Returns index of item with given path. |
|
Returns index of layer item with given uri. |
|
Hide the given path in the browser model |
|
Delayed initialization, needed because the provider registry must be already populated. |
|
Returns |
|
Emitted when an item's state is changed. |
|
Refresh item specified by path |
|
Refreshes the list of drive items, removing any corresponding to removed drives and adding newly added drives. |
|
Reload the whole model |
|
Removes a favorite directory from its corresponding model index. |
|
Returns the root items for the model. |
|
Signals
Emitted when connections for the specified providerKey have changed in the browser. |
|
Emitted when item children fetch was finished |
- 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.mPathAvailable as
QgsBrowserModel.PathRole
in older QGIS releases.Comment
: Item commentAvailable 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 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(providerKey: str)[source]¶
Emitted when connections for the specified
providerKey
have changed in the browser.Forwarded to the widget and used to notify the provider dialogs of a changed connection.
- Parameters:
providerKey (str)
- 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
- 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 ofparent
are searched. If noparent
is 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
True
if 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
- Parameters:
path (Optional[str])
- refresh(self, index: QModelIndex = QModelIndex())[source]
Refresh item children
- Parameters:
index (QModelIndex = QModelIndex())
- 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:
List[QgsDataItem]
- signal stateChanged(index: QModelIndex, oldState: Qgis.BrowserItemState)[source]¶
Emitted when item children fetch was finished
- Parameters:
index (QModelIndex)
oldState (Qgis.BrowserItemState)