Class: QgsDataItem

Base class for all items in the model.

Parent/children hierarchy is not based on QObject.

Class Hierarchy

Inheritance diagram of qgis.core.QgsDataItem

Base classes

QObject

Subclasses

QgsDataCollectionItem

A browser item for collections of data.

QgsErrorItem

A browser item that can be used to report problems (e.g. network error).

QgsFieldDomainItem

A browser item representing a field domain.

QgsFieldDomainsItem

Contains a collection of field domain items.

QgsFieldItem

A data item representing a single field from a layer.

QgsFieldsItem

A browser item which contains a collection of field items.

QgsLayerItem

A browser item that represents a layer that can be opened with one of the providers.

QgsProjectItem

Data item that can be used to represent QGIS projects.

QgsRelationshipItem

A browser item representing a relationship.

QgsRelationshipsItem

Contains a collection of relationship items.

Enums

Capability

alias of BrowserItemCapability

State

alias of BrowserItemState

Type

alias of BrowserItemType

Methods

deferredDelete

The item is scheduled to be deleted.

deleteLater

hasChildren

moveToThread

Move object and all its descendants to thread

name

Returns the name of the item (the displayed text for the item).

parent

Gets item parent.

path

providerKey

Returns the provider key that created this item (e.g. "PostGIS").

rowCount

setCapabilities

setCapabilitiesV2

Sets the capabilities for the data item.

setIcon

setIconName

setName

Sets the name of the item (the displayed text for the item).

setParent

Set item parent and connect / disconnect parent to / from item signals.

setPath

setProviderKey

Sets the provider key that created this item (e.g. "PostGIS").

setSortKey

Sets a custom sorting key for the item.

setToolTip

state

toolTip

type

updateIcon

Will request a repaint of this icon.

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsDataItem. See the FAQ for more details.

acceptDrop

Returns whether the item accepts drag and dropped layers - e.g. for importing a dataset to a provider.

actions

Returns the list of actions available for this item.

addChildItem

Inserts a new child item.

capabilities2

Returns the capabilities for the data item.

childrenCreated

createChildren

Create children.

databaseConnection

For data items that represent a DB connection or one of its children, this method returns a connection.

deleteChildItem

Removes and deletes a child item, emitting relevant signals to the model.

depopulate

Remove children recursively and set as not populated.

equal

Returns True if this item is equal to another item (by testing item type and path).

handleDoubleClick

Called when a user double clicks on the item.

handleDrop

Attempts to process the mime data dropped on this item.

hasDragEnabled

Returns True if the item may be dragged.

icon

layerCollection

Returns True if the data item is a collection of layers The default implementation returns False, subclasses must implement this method if their children are layers.

menus

Returns the list of menus available for this item.

mimeUri

Returns mime URI for the data item.

mimeUris

Returns mime URIs for the data item, most data providers will only return a single URI but some data collection items (e.g. GPKG, OGR) may report multiple URIs (e.g. for vector and raster layer types).

paramWidget

Returns source widget from data item for QgsBrowserPropertiesWidget

populate

refresh

Refresh the items from a specified list of child items.

refreshConnections

Causes a data item provider to refresh all registered connections.

removeChildItem

Removes a child item and returns it without deleting it.

rename

Sets a new name for the item, and returns True if the item was successfully renamed.

setCrs

Writes the selected crs into data source.

setState

Set item state.

sortKey

Returns the sorting key for the item.

Static Methods

findItem

pathComponent

Create path component replacing path separators

Signals

beginInsertItems

Emitted before child items are added to this data item.

beginRemoveItems

Emitted before child items are removed from this data item.

connectionsChanged

Emitted when the connections of the provider with the specified providerKey have changed.

dataChanged

Emitted when data changes for an item.

endInsertItems

Emitted after child items have been added to this data item.

endRemoveItems

Emitted after child items have been removed from this data item.

stateChanged

Emitted when an item's state is changed.

class qgis.core.QgsDataItem[source]

Bases: QObject

__init__(type: Qgis.BrowserItemType, parent: QgsDataItem | None, name: str | None, path: str | None, providerKey: str | None = '')

Constructor for QgsDataItem, with the specified parent item.

The name argument specifies the text to show in the model for the item. A translated string should be used wherever appropriate.

The path argument gives the item path in the browser tree. The path string can take any form, but QgsDataItem items pointing to different logical locations should always use a different item path.

The optional providerKey string (added in QGIS 3.12) can be used to specify the key for the QgsDataItemProvider that created this item.

Parameters:
Capabilities

alias of BrowserItemCapabilities

Capability

alias of BrowserItemCapability

State

alias of BrowserItemState

Type

alias of BrowserItemType

virtual acceptDrop(self) bool[source]

Returns whether the item accepts drag and dropped layers - e.g. for importing a dataset to a provider. Subclasses should override this and handleDrop() to accept dropped layers.

See also

handleDrop()

See also

QgsDataItemGuiProvider.handleDrop()

Deprecated since version 3.10.

Return type:

bool

virtual actions(self, parent: QWidget | None) List[QAction]

Returns the list of actions available for this item. This is usually used for the popup menu on right-clicking the item. Subclasses should override this to provide actions.

Subclasses should ensure that ownership of created actions is correctly handled by parenting them to the specified parent widget.

Parameters:

parent (Optional[QWidget])

Return type:

List[QAction]

virtual addChildItem(self, child: QgsDataItem | None, refresh: bool = False)[source]

Inserts a new child item. The child will be inserted at a position using an alphabetical order based on mName.

Parameters:
  • child (Optional[QgsDataItem]) – child item to insert. Ownership is transferred, and item parent will be set and relevant connections made.

  • refresh (bool = False) – set to True to refresh populated item, emitting relevant signals to the model

signal beginInsertItems(parent: QgsDataItem, first: int, last: int)[source]

Emitted before child items are added to this data item.

This signal must be followed by endInsertItems().

Parameters:
  • parent (QgsDataItem) – the parent item having children added, will always be this object

  • first (int) – index of first child item to be added

  • last (int) – index last child item, after the addition has occurred

See also

endInsertItems()

signal beginRemoveItems(parent: QgsDataItem, first: int, last: int)[source]

Emitted before child items are removed from this data item.

This signal must be followed by endRemoveItems().

Parameters:
  • parent (QgsDataItem) – the parent item having children removed, will always be this object

  • first (int) – index of first child item to be removed

  • last (int) – index of the last child item to be removed

See also

endRemoveItems()

virtual capabilities2(self) Qgis.BrowserItemCapabilities[source]

Returns the capabilities for the data item.

Return type:

Qgis.BrowserItemCapabilities

virtual childrenCreated(self)[source]
signal connectionsChanged(providerKey: str | None = None)[source]

Emitted when the connections of the provider with the specified providerKey have changed.

This signal is normally forwarded to the app in order to refresh the connection item in the provider dialogs and to refresh the connection items in the other open browsers.

Parameters:

providerKey (Optional[str] = None)

virtual createChildren(self) List[QgsDataItem]

Create children. Children are not expected to have parent set.

Warning

This method MUST BE THREAD SAFE.

Return type:

List[QgsDataItem]

signal dataChanged(item: QgsDataItem)[source]

Emitted when data changes for an item.

Parameters:

item (QgsDataItem)

virtual databaseConnection(self) QgsAbstractDatabaseProviderConnection | None[source]

For data items that represent a DB connection or one of its children, this method returns a connection. All other data items will return NULL.

Ownership of the returned objects is transferred to the caller.

Added in version 3.16.

Return type:

Optional[QgsAbstractDatabaseProviderConnection]

deferredDelete(self) bool[source]

The item is scheduled to be deleted. E.g. if deleteLater() is called when item is in Populating state (createChildren() running in another thread), the deferredDelete() returns True and item will be deleted once Populating finished. Items with slow reateChildren() (for example network or database based) may check during createChildren() if deferredDelete() returns True and return from createChildren() immediately because result will be useless.

Return type:

bool

virtual deleteChildItem(self, child: QgsDataItem | None)[source]

Removes and deletes a child item, emitting relevant signals to the model.

Parameters:

child (Optional[QgsDataItem]) – child to remove. Item must exist as a current child.

See also

addChildItem()

deleteLater(items: Iterable[QgsDataItem])[source]
Parameters:

items (Iterable[QgsDataItem])

deleteLater(self)[source]

Safely delete the item:

  • disconnects parent

  • unsets parent (but does not remove itself)

  • deletes all its descendants recursively

  • waits until Populating state (createChildren() in thread) finished without blocking main thread

  • calls QObject.deleteLater()

virtual depopulate(self)[source]

Remove children recursively and set as not populated. This is used when refreshing collapsed items.

signal endInsertItems[source]

Emitted after child items have been added to this data item.

This signal will always be preceded by beginInsertItems().

signal endRemoveItems[source]

Emitted after child items have been removed from this data item.

This signal will always be preceded by beginRemoveItems().

virtual equal(self, other: QgsDataItem | None) bool[source]

Returns True if this item is equal to another item (by testing item type and path).

Parameters:

other (Optional[QgsDataItem])

Return type:

bool

static findItem(items: Iterable[QgsDataItem], item: QgsDataItem | None) int[source]
Parameters:
Return type:

int

virtual handleDoubleClick(self) bool[source]

Called when a user double clicks on the item. Subclasses should return True if they have implemented a double-click handler and do not want the default double-click behavior for items.

Return type:

bool

virtual handleDrop(self, a0: QMimeData | None, a1: Qt.DropAction) bool[source]

Attempts to process the mime data dropped on this item. Subclasses must override this and acceptDrop() if they accept dropped layers.

See also

acceptDrop()

See also

QgsDataItemGuiProvider.handleDrop()

Deprecated since version 3.10.

Parameters:
  • a0 (Optional[QMimeData])

  • a1 (Qt.DropAction)

Return type:

bool

hasChildren(self) bool[source]
Return type:

bool

virtual hasDragEnabled(self) bool[source]

Returns True if the item may be dragged. Default implementation returns False. A draggable item has to implement mimeUris() that will be used to pass data.

See also

mimeUris()

Return type:

bool

virtual icon(self) QIcon[source]
Return type:

QIcon

virtual layerCollection(self) bool[source]

Returns True if the data item is a collection of layers The default implementation returns False, subclasses must implement this method if their children are layers.

Added in version 3.14.

Return type:

bool

virtual menus(self, parent: QWidget | None) List[QMenu]

Returns the list of menus available for this item. This is usually used for the popup menu on right-clicking the item. Subclasses should override this to provide actions. Subclasses should ensure that ownership of created menus is correctly handled by parenting them to the specified parent widget.

Parameters:

parent (Optional[QWidget]) – a parent widget of the menu

Return type:

List[QMenu]

Returns:

list of menus

virtual mimeUri(self) QgsMimeDataUtils.Uri[source]

Returns mime URI for the data item. Items that return valid URI will be returned in mime data when dragging a selection from browser model.

See also

hasDragEnabled()

Deprecated since version 3.18: Use mimeUris() instead.

Return type:

QgsMimeDataUtils.Uri

virtual mimeUris(self) List[QgsMimeDataUtils.Uri][source]

Returns mime URIs for the data item, most data providers will only return a single URI but some data collection items (e.g. GPKG, OGR) may report multiple URIs (e.g. for vector and raster layer types).

Items that return valid URI will be returned in mime data when dragging a selection from browser model.

Added in version 3.18.

Return type:

List[QgsMimeDataUtils.Uri]

moveToThread(self, targetThread: QThread | None)[source]

Move object and all its descendants to thread

Parameters:

targetThread (Optional[QThread])

name(self) str[source]

Returns the name of the item (the displayed text for the item).

See also

setName()

Return type:

str

virtual paramWidget(self) QWidget | None[source]

Returns source widget from data item for QgsBrowserPropertiesWidget

Use QgsDataItemGuiProvider.createParamWidget() instead

Deprecated since version 3.10.

Return type:

Optional[QWidget]

parent(self) QgsDataItem | None[source]

Gets item parent. QgsDataItem maintains its own items hierarchy, it does not use QObject hierarchy.

Return type:

Optional[QgsDataItem]

path(self) str[source]
Return type:

str

static pathComponent(component: str | None) str[source]

Create path component replacing path separators

Parameters:

component (Optional[str])

Return type:

str

virtual populate(self, children: Iterable[QgsDataItem])[source]
virtual populate(self, foreground: bool = False) None
Parameters:

children (Iterable[QgsDataItem])

virtual populate(self, foreground: bool = False)[source]
Parameters:

foreground (bool = False)

providerKey(self) str[source]

Returns the provider key that created this item (e.g. “PostGIS”)

If key has a prefix “special:”, it marks that the item was not created with a provider, but manually. For example “special:Favorites”, “special:Home”

Added in version 3.12.

Return type:

str

virtual refresh(self, children: Iterable[QgsDataItem])[source]

Refresh the items from a specified list of child items.

Parameters:

children (Iterable[QgsDataItem])

virtual refresh(self)[source]
virtual refreshConnections(self, providerKey: str | None = '')[source]

Causes a data item provider to refresh all registered connections.

If providerKey is specified then only the matching provider will be refreshed. Otherwise, all providers will be refreshed (which is potentially very expensive!).

Parameters:

providerKey (Optional[str] = '')

virtual removeChildItem(self, child: QgsDataItem | None) QgsDataItem | None[source]

Removes a child item and returns it without deleting it. Emits relevant signals to model as required.

Parameters:

child (Optional[QgsDataItem]) – child to remove

Return type:

Optional[QgsDataItem]

Returns:

pointer to the removed item or None if no such item was found

virtual rename(self, name: str | None) bool[source]

Sets a new name for the item, and returns True if the item was successfully renamed.

Items which implement this method should return the QgsDataItem.Rename capability.

The default implementation does nothing.

Use QgsDataItemGuiProvider:

Added in version 3.4.

Deprecated since version 3.10.

Parameters:

name (Optional[str])

Return type:

bool

rowCount(self) int[source]
Return type:

int

setCapabilities(self, capabilities: int)[source]

Deprecated since version 3.40.

Use setCapabilitiesV2() instead.

Parameters:

capabilities (int)

setCapabilitiesV2(self, capabilities: Qgis.BrowserItemCapabilities | Qgis.BrowserItemCapability)

Sets the capabilities for the data item.

See also

capabilities2()

Parameters:

capabilities (Union[Qgis.BrowserItemCapabilities, Qgis.BrowserItemCapability])

virtual setCrs(self, crs: QgsCoordinateReferenceSystem) bool[source]

Writes the selected crs into data source. The original data source will be modified when calling this method.

Deprecated since version 3.6: This method is no longer used by QGIS and will be removed in QGIS 4.0.

Parameters:

crs (QgsCoordinateReferenceSystem)

Return type:

bool

setIcon(self, icon: QIcon)[source]
Parameters:

icon (QIcon)

setIconName(self, iconName: str | None)[source]
Parameters:

iconName (Optional[str])

setName(self, name: str | None)[source]

Sets the name of the item (the displayed text for the item).

See also

name()

Parameters:

name (Optional[str])

setParent(self, parent: QgsDataItem | None)[source]

Set item parent and connect / disconnect parent to / from item signals. It does not add itself to parents children (mChildren)

Parameters:

parent (Optional[QgsDataItem])

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

path (Optional[str])

setProviderKey(self, value: str | None)[source]

Sets the provider key that created this item (e.g. “PostGIS”)

If key has a prefix “special:”, it marks that the item was not created with a provider, but manually. For example “special:Favorites”

Added in version 3.12.

Parameters:

value (Optional[str])

setSortKey(self, key: Any)[source]

Sets a custom sorting key for the item.

See also

sortKey()

Parameters:

key (Any)

virtual setState(self, state: Qgis.BrowserItemState)[source]

Set item state. It also take care about starting/stopping loading icon animation.

Parameters:

state (Qgis.BrowserItemState)

setToolTip(self, msg: str | None)[source]
Parameters:

msg (Optional[str])

virtual sortKey(self) Any[source]

Returns the sorting key for the item. By default name() is returned, but setSortKey() can be used to set a custom sort key for the item.

Alternatively subclasses can override this method to return a custom sort key.

See also

setSortKey()

Return type:

Any

state(self) Qgis.BrowserItemState[source]
Return type:

Qgis.BrowserItemState

signal stateChanged(item: QgsDataItem, oldState: Qgis.BrowserItemState)[source]

Emitted when an item’s state is changed.

Parameters:
toolTip(self) str[source]
Return type:

str

type(self) Qgis.BrowserItemType[source]
Return type:

Qgis.BrowserItemType

updateIcon(self)[source]

Will request a repaint of this icon.