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.

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

signal beginRemoveItems[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.

virtual capabilities2(self) Qgis.BrowserItemCapabilities[source]

Returns the capabilities for the data item.

Return type:

Qgis.BrowserItemCapabilities

virtual childrenCreated(self)[source]
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.

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

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]

Returns True if 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]

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]

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.

signal endRemoveItems[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.

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]

Returns whether this item has children.

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)
Parameters:

children (Iterable[QgsDataItem])

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.

refresh(self)

Parameters:

children (Iterable[QgsDataItem])

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]

Returns the number of rows of this item

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

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.