QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
Public Slots | Signals | Public Member Functions | Protected Slots | Protected Member Functions | Protected Attributes | Friends | List of all members
QgsLayerTreeView Class Reference

The QgsLayerTreeView class extends QTreeView and provides some additional functionality when working with a layer tree. More...

#include <qgslayertreeview.h>

Inheritance diagram for QgsLayerTreeView:
Inheritance graph
[legend]

Public Slots

void collapseAllNodes ()
 Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes.
 
void expandAllNodes ()
 Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes.
 
void refreshLayerSymbology (const QString &layerId)
 Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model.
 
void setHideValidLayers (bool hideValid)
 Sets whether valid layers should be hidden (i.e.
 
void setLayerMarkWidth (int width)
 Set width of contextual menu mark, at right of layer node items.
 
void setMessageBar (QgsMessageBar *messageBar)
 Set the message bar to display messages from the layer tree.
 
void setShowPrivateLayers (bool showPrivate)
 Set the show private layers to showPrivate.
 

Signals

void contextMenuAboutToShow (QMenu *menu)
 Emitted when the context menu is about to show.
 
void currentLayerChanged (QgsMapLayer *layer)
 Emitted when a current layer is changed.
 
void datasetsDropped (QDropEvent *event)
 Emitted when datasets are dropped onto the layer tree view.
 

Public Member Functions

 QgsLayerTreeView (QWidget *parent=nullptr)
 Constructor for QgsLayerTreeView.
 
 ~QgsLayerTreeView () override
 
void addIndicator (QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator)
 Adds an indicator to the given layer tree node.
 
QgsLayerTreeGroupcurrentGroupNode () const
 Gets current group node. If a layer is current node, the function will return parent group. May be nullptr.
 
QgsMapLayercurrentLayer () const
 Returns the currently selected layer, or nullptr if no layers is selected.
 
QgsLayerTreeModelLegendNodecurrentLegendNode () const
 Gets current legend node.
 
QgsLayerTreeNodecurrentNode () const
 Gets current node. May be nullptr.
 
QgsLayerTreeViewDefaultActionsdefaultActions ()
 Gets access to the default actions that may be used with the tree view.
 
bool hideValidLayers () const
 Returns if valid layers should be hidden (i.e.
 
QgsLayerTreeModelLegendNodeindex2legendNode (const QModelIndex &index) const
 Returns legend node for given proxy model tree index.
 
QgsLayerTreeNodeindex2node (const QModelIndex &index) const
 Returns layer tree node for given proxy model tree index.
 
QList< QgsLayerTreeViewIndicator * > indicators (QgsLayerTreeNode *node) const
 Returns list of indicators associated with a particular layer tree node.
 
int layerMarkWidth () const
 Returns width of contextual menu mark, at right of layer node items.
 
QgsLayerTreeModellayerTreeModel () const
 Gets access to the model casted to QgsLayerTreeModel.
 
QModelIndex legendNode2index (QgsLayerTreeModelLegendNode *legendNode)
 Returns proxy model index for a given legend node.
 
QModelIndex legendNode2sourceIndex (QgsLayerTreeModelLegendNode *legendNode)
 Returns index for a given legend node.
 
QgsLayerTreeViewMenuProvidermenuProvider () const
 Returns pointer to the context menu provider. May be nullptr.
 
QModelIndex node2index (QgsLayerTreeNode *node) const
 Returns proxy model index for a given node.
 
QModelIndex node2sourceIndex (QgsLayerTreeNode *node) const
 Returns source model index for a given node.
 
QgsLayerTreeProxyModelproxyModel () const
 Returns the proxy model used by the view.
 
void removeIndicator (QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator)
 Removes a previously added indicator to a layer tree node.
 
QList< QgsLayerTreeLayer * > selectedLayerNodes () const
 Returns the list of selected nodes filtered to just layer nodes (QgsLayerTreeLayer).
 
QList< QgsMapLayer * > selectedLayers () const
 Returns the list of selected layers.
 
QList< QgsMapLayer * > selectedLayersRecursive () const
 Gets list of selected layers, including those that are not directly selected, but their ancestor groups is selected.
 
QList< QgsLayerTreeModelLegendNode * > selectedLegendNodes () const
 Returns the list of selected legend nodes.
 
QList< QgsLayerTreeNode * > selectedNodes (bool skipInternal=false) const
 Returns the list of selected layer tree nodes.
 
void setCurrentLayer (QgsMapLayer *layer)
 Sets the currently selected layer.
 
void setCurrentNode (QgsLayerTreeNode *node)
 Sets the currently selected node.
 
void setLayerVisible (QgsMapLayer *layer, bool visible)
 Convenience methods which sets the visible state of the specified map layer.
 
void setMenuProvider (QgsLayerTreeViewMenuProvider *menuProvider)
 Sets provider for context menu. Takes ownership of the instance.
 
void setModel (QAbstractItemModel *model) override
 Overridden setModel() from base class. Only QgsLayerTreeModel is an acceptable model.
 
bool showPrivateLayers () const
 Returns the show private layers status.
 

Protected Slots

void modelRowsInserted (const QModelIndex &index, int start, int end)
 
void modelRowsRemoved ()
 
void onCurrentChanged ()
 
void onExpandedChanged (QgsLayerTreeNode *node, bool expanded)
 
void onModelReset ()
 
void updateExpandedStateToNode (const QModelIndex &index)
 

Protected Member Functions

void contextMenuEvent (QContextMenuEvent *event) override
 
void dragEnterEvent (QDragEnterEvent *event) override
 
void dragMoveEvent (QDragMoveEvent *event) override
 
void dropEvent (QDropEvent *event) override
 
void keyPressEvent (QKeyEvent *event) override
 
QgsMapLayerlayerForIndex (const QModelIndex &index) const
 
void mouseDoubleClickEvent (QMouseEvent *event) override
 
void mouseReleaseEvent (QMouseEvent *event) override
 
void resizeEvent (QResizeEvent *event) override
 
void updateExpandedStateFromNode (QgsLayerTreeNode *node)
 

Protected Attributes

QString mCurrentLayerID
 Keeps track of current layer ID (to check when to emit signal about change of current layer)
 
QgsLayerTreeViewDefaultActionsmDefaultActions = nullptr
 helper class with default actions. Lazily initialized.
 
QHash< QgsLayerTreeNode *, QList< QgsLayerTreeViewIndicator * > > mIndicators
 Storage of indicators used with the tree view.
 
QPoint mLastReleaseMousePos
 Used by the item delegate for identification of which indicator has been clicked.
 
int mLayerMarkWidth
 Width of contextual menu mark for layer nodes.
 
QgsLayerTreeViewMenuProvidermMenuProvider = nullptr
 Context menu provider. Owned by the view.
 

Friends

class QgsLayerTreeViewItemDelegate
 

Detailed Description

The QgsLayerTreeView class extends QTreeView and provides some additional functionality when working with a layer tree.

The view updates expanded state of layer tree nodes and also listens to changes to expanded states in the layer tree.

The view keeps track of the current layer and emits a signal when the current layer has changed.

Allows the client to specify a context menu provider with custom actions. Also it comes with a set of default actions that can be used when building context menu.

See also
QgsLayerTreeModel

Definition at line 119 of file qgslayertreeview.h.

Constructor & Destructor Documentation

◆ QgsLayerTreeView()

QgsLayerTreeView::QgsLayerTreeView ( QWidget *  parent = nullptr)
explicit

Constructor for QgsLayerTreeView.

Definition at line 45 of file qgslayertreeview.cpp.

◆ ~QgsLayerTreeView()

QgsLayerTreeView::~QgsLayerTreeView ( )
override

Definition at line 82 of file qgslayertreeview.cpp.

Member Function Documentation

◆ addIndicator()

void QgsLayerTreeView::addIndicator ( QgsLayerTreeNode node,
QgsLayerTreeViewIndicator indicator 
)

Adds an indicator to the given layer tree node.

Indicators are icons shown next to layer/group names in the layer tree view. They can be used to show extra information with tree nodes and they allow user interaction.

Does not take ownership of the indicator. One indicator object may be used for multiple layer tree nodes.

See also
removeIndicator
indicators
Since
QGIS 3.2

Definition at line 506 of file qgslayertreeview.cpp.

◆ collapseAllNodes

void QgsLayerTreeView::collapseAllNodes ( )
slot

Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes.

Definition at line 587 of file qgslayertreeview.cpp.

◆ contextMenuAboutToShow

void QgsLayerTreeView::contextMenuAboutToShow ( QMenu *  menu)
signal

Emitted when the context menu is about to show.

Allows customization of the menu.

Since
QGIS 3.32

◆ contextMenuEvent()

void QgsLayerTreeView::contextMenuEvent ( QContextMenuEvent *  event)
overrideprotected

Definition at line 191 of file qgslayertreeview.cpp.

◆ currentGroupNode()

QgsLayerTreeGroup * QgsLayerTreeView::currentGroupNode ( ) const

Gets current group node. If a layer is current node, the function will return parent group. May be nullptr.

Definition at line 412 of file qgslayertreeview.cpp.

◆ currentLayer()

QgsMapLayer * QgsLayerTreeView::currentLayer ( ) const

Returns the currently selected layer, or nullptr if no layers is selected.

See also
setCurrentLayer()

Definition at line 150 of file qgslayertreeview.cpp.

◆ currentLayerChanged

void QgsLayerTreeView::currentLayerChanged ( QgsMapLayer layer)
signal

Emitted when a current layer is changed.

◆ currentLegendNode()

QgsLayerTreeModelLegendNode * QgsLayerTreeView::currentLegendNode ( ) const

Gets current legend node.

May be nullptr if current node is not a legend node.

Definition at line 434 of file qgslayertreeview.cpp.

◆ currentNode()

QgsLayerTreeNode * QgsLayerTreeView::currentNode ( ) const

Gets current node. May be nullptr.

Definition at line 407 of file qgslayertreeview.cpp.

◆ datasetsDropped

void QgsLayerTreeView::datasetsDropped ( QDropEvent *  event)
signal

Emitted when datasets are dropped onto the layer tree view.

◆ defaultActions()

QgsLayerTreeViewDefaultActions * QgsLayerTreeView::defaultActions ( )

Gets access to the default actions that may be used with the tree view.

Definition at line 137 of file qgslayertreeview.cpp.

◆ dragEnterEvent()

void QgsLayerTreeView::dragEnterEvent ( QDragEnterEvent *  event)
overrideprotected

Definition at line 684 of file qgslayertreeview.cpp.

◆ dragMoveEvent()

void QgsLayerTreeView::dragMoveEvent ( QDragMoveEvent *  event)
overrideprotected

Definition at line 698 of file qgslayertreeview.cpp.

◆ dropEvent()

void QgsLayerTreeView::dropEvent ( QDropEvent *  event)
overrideprotected

Definition at line 712 of file qgslayertreeview.cpp.

◆ expandAllNodes

void QgsLayerTreeView::expandAllNodes ( )
slot

Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes.

Definition at line 580 of file qgslayertreeview.cpp.

◆ hideValidLayers()

bool QgsLayerTreeView::hideValidLayers ( ) const

Returns if valid layers should be hidden (i.e.

only invalid layers are shown).

See also
setHideValidLayers()
Since
QGIS 3.38

Definition at line 628 of file qgslayertreeview.cpp.

◆ index2legendNode()

QgsLayerTreeModelLegendNode * QgsLayerTreeView::index2legendNode ( const QModelIndex &  index) const

Returns legend node for given proxy model tree index.

Returns nullptr for invalid index

Unlike QgsLayerTreeModel::index2legendNode(), calling this method correctly accounts for mapping the view indexes through the view's proxy model to the source model.

Since
QGIS 3.18

Definition at line 822 of file qgslayertreeview.cpp.

◆ index2node()

QgsLayerTreeNode * QgsLayerTreeView::index2node ( const QModelIndex &  index) const

Returns layer tree node for given proxy model tree index.

Returns root node for invalid index. Returns nullptr if index does not refer to a layer tree node (e.g. it is a legend node)

Unlike QgsLayerTreeModel::index2Node(), calling this method correctly accounts for mapping the view indexes through the view's proxy model to the source model.

Since
QGIS 3.18

Definition at line 807 of file qgslayertreeview.cpp.

◆ indicators()

QList< QgsLayerTreeViewIndicator * > QgsLayerTreeView::indicators ( QgsLayerTreeNode node) const

Returns list of indicators associated with a particular layer tree node.

See also
addIndicator
removeIndicator
Since
QGIS 3.2

Definition at line 527 of file qgslayertreeview.cpp.

◆ keyPressEvent()

void QgsLayerTreeView::keyPressEvent ( QKeyEvent *  event)
overrideprotected

Definition at line 656 of file qgslayertreeview.cpp.

◆ layerForIndex()

QgsMapLayer * QgsLayerTreeView::layerForIndex ( const QModelIndex &  index) const
protected

Definition at line 385 of file qgslayertreeview.cpp.

◆ layerMarkWidth()

int QgsLayerTreeView::layerMarkWidth ( ) const
inline

Returns width of contextual menu mark, at right of layer node items.

See also
setLayerMarkWidth
Since
QGIS 3.8

Definition at line 345 of file qgslayertreeview.h.

◆ layerTreeModel()

QgsLayerTreeModel * QgsLayerTreeView::layerTreeModel ( ) const

Gets access to the model casted to QgsLayerTreeModel.

Definition at line 132 of file qgslayertreeview.cpp.

◆ legendNode2index()

QModelIndex QgsLayerTreeView::legendNode2index ( QgsLayerTreeModelLegendNode legendNode)

Returns proxy model index for a given legend node.

If the legend node does not belong to the layer tree, the result is undefined. If the legend node is belongs to the tree but it is filtered out, invalid model index is returned.

Unlike QgsLayerTreeModel::legendNode2index(), calling this method correctly accounts for mapping the view indexes through the view's proxy model to the source model.

Since
QGIS 3.18

Definition at line 827 of file qgslayertreeview.cpp.

◆ legendNode2sourceIndex()

QModelIndex QgsLayerTreeView::legendNode2sourceIndex ( QgsLayerTreeModelLegendNode legendNode)

Returns index for a given legend node.

If the legend node does not belong to the layer tree, the result is undefined. If the legend node is belongs to the tree but it is filtered out, invalid model index is returned.

Since
QGIS 3.18

Definition at line 832 of file qgslayertreeview.cpp.

◆ menuProvider()

QgsLayerTreeViewMenuProvider * QgsLayerTreeView::menuProvider ( ) const
inline

Returns pointer to the context menu provider. May be nullptr.

Definition at line 219 of file qgslayertreeview.h.

◆ modelRowsInserted

void QgsLayerTreeView::modelRowsInserted ( const QModelIndex &  index,
int  start,
int  end 
)
protectedslot

Definition at line 212 of file qgslayertreeview.cpp.

◆ modelRowsRemoved

void QgsLayerTreeView::modelRowsRemoved ( )
protectedslot

Definition at line 283 of file qgslayertreeview.cpp.

◆ mouseDoubleClickEvent()

void QgsLayerTreeView::mouseDoubleClickEvent ( QMouseEvent *  event)
overrideprotected

Definition at line 633 of file qgslayertreeview.cpp.

◆ mouseReleaseEvent()

void QgsLayerTreeView::mouseReleaseEvent ( QMouseEvent *  event)
overrideprotected

Definition at line 641 of file qgslayertreeview.cpp.

◆ node2index()

QModelIndex QgsLayerTreeView::node2index ( QgsLayerTreeNode node) const

Returns proxy model index for a given node.

If the node does not belong to the layer tree, the result is undefined

Unlike QgsLayerTreeModel::node2index(), calling this method correctly accounts for mapping the view indexes through the view's proxy model to the source model.

Since
QGIS 3.18

Definition at line 812 of file qgslayertreeview.cpp.

◆ node2sourceIndex()

QModelIndex QgsLayerTreeView::node2sourceIndex ( QgsLayerTreeNode node) const

Returns source model index for a given node.

If the node does not belong to the layer tree, the result is undefined

Since
QGIS 3.18

Definition at line 817 of file qgslayertreeview.cpp.

◆ onCurrentChanged

void QgsLayerTreeView::onCurrentChanged ( )
protectedslot

Definition at line 314 of file qgslayertreeview.cpp.

◆ onExpandedChanged

void QgsLayerTreeView::onExpandedChanged ( QgsLayerTreeNode node,
bool  expanded 
)
protectedslot

Definition at line 346 of file qgslayertreeview.cpp.

◆ onModelReset

void QgsLayerTreeView::onModelReset ( )
protectedslot

Definition at line 369 of file qgslayertreeview.cpp.

◆ proxyModel()

QgsLayerTreeProxyModel * QgsLayerTreeView::proxyModel ( ) const

Returns the proxy model used by the view.

This can be used to set filters controlling which layers are shown in the view.

Since
QGIS 3.18

Definition at line 802 of file qgslayertreeview.cpp.

◆ refreshLayerSymbology

void QgsLayerTreeView::refreshLayerSymbology ( const QString &  layerId)
slot

Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model.

Definition at line 539 of file qgslayertreeview.cpp.

◆ removeIndicator()

void QgsLayerTreeView::removeIndicator ( QgsLayerTreeNode node,
QgsLayerTreeViewIndicator indicator 
)

Removes a previously added indicator to a layer tree node.

Does not delete the indicator.

See also
addIndicator
indicators
Since
QGIS 3.2

Definition at line 521 of file qgslayertreeview.cpp.

◆ resizeEvent()

void QgsLayerTreeView::resizeEvent ( QResizeEvent *  event)
overrideprotected

Definition at line 738 of file qgslayertreeview.cpp.

◆ selectedLayerNodes()

QList< QgsLayerTreeLayer * > QgsLayerTreeView::selectedLayerNodes ( ) const

Returns the list of selected nodes filtered to just layer nodes (QgsLayerTreeLayer).

See also
selectedNodes()
selectedLayers()
selectedLegendNodes()

Definition at line 450 of file qgslayertreeview.cpp.

◆ selectedLayers()

QList< QgsMapLayer * > QgsLayerTreeView::selectedLayers ( ) const

Returns the list of selected layers.

See also
selectedNodes()
selectedLayerNodes()
selectedLegendNodes()

Definition at line 463 of file qgslayertreeview.cpp.

◆ selectedLayersRecursive()

QList< QgsMapLayer * > QgsLayerTreeView::selectedLayersRecursive ( ) const

Gets list of selected layers, including those that are not directly selected, but their ancestor groups is selected.

If we have a group with two layers L1, L2 and just the group node is selected, this method returns L1 and L2, while selectedLayers() returns an empty list.

Since
QGIS 3.4

Definition at line 493 of file qgslayertreeview.cpp.

◆ selectedLegendNodes()

QList< QgsLayerTreeModelLegendNode * > QgsLayerTreeView::selectedLegendNodes ( ) const

Returns the list of selected legend nodes.

See also
selectedNodes()
selectedLayerNodes()
Since
QGIS 3.32

Definition at line 476 of file qgslayertreeview.cpp.

◆ selectedNodes()

QList< QgsLayerTreeNode * > QgsLayerTreeView::selectedNodes ( bool  skipInternal = false) const

Returns the list of selected layer tree nodes.

Parameters
skipInternalIf true, will ignore nodes which have an ancestor in the selection
See also
selectedLayerNodes()
selectedLegendNodes()
selectedLayers()

Definition at line 439 of file qgslayertreeview.cpp.

◆ setCurrentLayer()

void QgsLayerTreeView::setCurrentLayer ( QgsMapLayer layer)

Sets the currently selected layer.

If layer is nullptr then all layers will be deselected.

See also
currentLayer()

Definition at line 166 of file qgslayertreeview.cpp.

◆ setCurrentNode()

void QgsLayerTreeView::setCurrentNode ( QgsLayerTreeNode node)

Sets the currently selected node.

If node is nullptr then all nodes will be deselected.

See also
currentNode()
Since
QGIS 3.40

Definition at line 155 of file qgslayertreeview.cpp.

◆ setHideValidLayers

void QgsLayerTreeView::setHideValidLayers ( bool  hideValid)
slot

Sets whether valid layers should be hidden (i.e.

only invalid layers are shown).

See also
setHideValidLayers()
Since
QGIS 3.38

Definition at line 617 of file qgslayertreeview.cpp.

◆ setLayerMarkWidth

void QgsLayerTreeView::setLayerMarkWidth ( int  width)
inlineslot

Set width of contextual menu mark, at right of layer node items.

See also
layerMarkWidth
Since
QGIS 3.8

Definition at line 395 of file qgslayertreeview.h.

◆ setLayerVisible()

void QgsLayerTreeView::setLayerVisible ( QgsMapLayer layer,
bool  visible 
)

Convenience methods which sets the visible state of the specified map layer.

See also
QgsLayerTreeNode::setItemVisibilityChecked()
Since
QGIS 3.10

Definition at line 181 of file qgslayertreeview.cpp.

◆ setMenuProvider()

void QgsLayerTreeView::setMenuProvider ( QgsLayerTreeViewMenuProvider menuProvider)

Sets provider for context menu. Takes ownership of the instance.

Definition at line 144 of file qgslayertreeview.cpp.

◆ setMessageBar

void QgsLayerTreeView::setMessageBar ( QgsMessageBar messageBar)
slot

Set the message bar to display messages from the layer tree.

Since
QGIS 3.14

Definition at line 594 of file qgslayertreeview.cpp.

◆ setModel()

void QgsLayerTreeView::setModel ( QAbstractItemModel *  model)
override

Overridden setModel() from base class. Only QgsLayerTreeModel is an acceptable model.

Definition at line 88 of file qgslayertreeview.cpp.

◆ setShowPrivateLayers

void QgsLayerTreeView::setShowPrivateLayers ( bool  showPrivate)
slot

Set the show private layers to showPrivate.

Since
QGIS 3.18

Definition at line 611 of file qgslayertreeview.cpp.

◆ showPrivateLayers()

bool QgsLayerTreeView::showPrivateLayers ( ) const

Returns the show private layers status.

Since
QGIS 3.18

Definition at line 623 of file qgslayertreeview.cpp.

◆ updateExpandedStateFromNode()

void QgsLayerTreeView::updateExpandedStateFromNode ( QgsLayerTreeNode node)
protected

Definition at line 375 of file qgslayertreeview.cpp.

◆ updateExpandedStateToNode

void QgsLayerTreeView::updateExpandedStateToNode ( const QModelIndex &  index)
protectedslot

Definition at line 289 of file qgslayertreeview.cpp.

Friends And Related Symbol Documentation

◆ QgsLayerTreeViewItemDelegate

friend class QgsLayerTreeViewItemDelegate
friend

Definition at line 496 of file qgslayertreeview.h.

Member Data Documentation

◆ mCurrentLayerID

QString QgsLayerTreeView::mCurrentLayerID
protected

Keeps track of current layer ID (to check when to emit signal about change of current layer)

Definition at line 474 of file qgslayertreeview.h.

◆ mDefaultActions

QgsLayerTreeViewDefaultActions* QgsLayerTreeView::mDefaultActions = nullptr
protected

helper class with default actions. Lazily initialized.

Definition at line 470 of file qgslayertreeview.h.

◆ mIndicators

QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > QgsLayerTreeView::mIndicators
protected

Storage of indicators used with the tree view.

Definition at line 476 of file qgslayertreeview.h.

◆ mLastReleaseMousePos

QPoint QgsLayerTreeView::mLastReleaseMousePos
protected

Used by the item delegate for identification of which indicator has been clicked.

Definition at line 478 of file qgslayertreeview.h.

◆ mLayerMarkWidth

int QgsLayerTreeView::mLayerMarkWidth
protected

Width of contextual menu mark for layer nodes.

Definition at line 481 of file qgslayertreeview.h.

◆ mMenuProvider

QgsLayerTreeViewMenuProvider* QgsLayerTreeView::mMenuProvider = nullptr
protected

Context menu provider. Owned by the view.

Definition at line 472 of file qgslayertreeview.h.


The documentation for this class was generated from the following files: