diff --git a/python/PyQt6/gui/auto_generated/layertree/qgslayertreeview.sip.in b/python/PyQt6/gui/auto_generated/layertree/qgslayertreeview.sip.in index e864350f5e39..cafd5688247e 100644 --- a/python/PyQt6/gui/auto_generated/layertree/qgslayertreeview.sip.in +++ b/python/PyQt6/gui/auto_generated/layertree/qgslayertreeview.sip.in @@ -139,7 +139,7 @@ for mapping the view indexes through the view's proxy model to the source model. QModelIndex node2index( QgsLayerTreeNode *node ) const; %Docstring -Returns proxy model index for a given node. If the node does not belong to the layer tree, the result is undefined +Returns proxy model index for a given node. If the node does not belong to the layer tree, the result is undefined Unlike :py:func:`QgsLayerTreeModel.node2index()`, calling this method correctly accounts for mapping the view indexes through the view's proxy model to the source model. @@ -150,7 +150,7 @@ for mapping the view indexes through the view's proxy model to the source model. QModelIndex node2sourceIndex( QgsLayerTreeNode *node ) const; %Docstring -Returns source model index for a given node. If the node does not belong to the layer tree, the result is undefined +Returns source model index for a given node. If the node does not belong to the layer tree, the result is undefined .. versionadded:: 3.18 %End @@ -213,6 +213,17 @@ Convenience methods which sets the visible state of the specified map ``layer``. .. seealso:: :py:func:`QgsLayerTreeNode.setItemVisibilityChecked` .. versionadded:: 3.10 +%End + + void setCurrentNode( QgsLayerTreeNode *node ); +%Docstring +Sets the currently selected ``node``. + +If ``node`` is ``None`` then all nodes will be deselected. + +.. seealso:: :py:func:`currentNode` + +.. versionadded:: 3.40 %End void setCurrentLayer( QgsMapLayer *layer ); @@ -361,7 +372,7 @@ Returns if valid layers should be hidden (i.e. only invalid layers are shown). public slots: void refreshLayerSymbology( const QString &layerId ); %Docstring -Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model +Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model %End void expandAllNodes(); diff --git a/src/gui/layertree/qgslayertreeview.cpp b/src/gui/layertree/qgslayertreeview.cpp index ba811db725b2..c132cfbaf6d0 100644 --- a/src/gui/layertree/qgslayertreeview.cpp +++ b/src/gui/layertree/qgslayertreeview.cpp @@ -151,6 +151,17 @@ QgsMapLayer *QgsLayerTreeView::currentLayer() const return layerForIndex( currentIndex() ); } +void QgsLayerTreeView::setCurrentNode( QgsLayerTreeNode *node ) +{ + if ( !node ) + { + setCurrentIndex( QModelIndex() ); + return; + } + + setCurrentIndex( node2index( node ) ); +} + void QgsLayerTreeView::setCurrentLayer( QgsMapLayer *layer ) { if ( !layer ) @@ -163,7 +174,7 @@ void QgsLayerTreeView::setCurrentLayer( QgsMapLayer *layer ) if ( !nodeLayer ) return; - setCurrentIndex( node2index( nodeLayer ) ); + setCurrentNode( nodeLayer ); } void QgsLayerTreeView::setLayerVisible( QgsMapLayer *layer, bool visible ) diff --git a/src/gui/layertree/qgslayertreeview.h b/src/gui/layertree/qgslayertreeview.h index f7aa3df435b8..8d00eaab7c3b 100644 --- a/src/gui/layertree/qgslayertreeview.h +++ b/src/gui/layertree/qgslayertreeview.h @@ -233,6 +233,16 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView */ void setLayerVisible( QgsMapLayer *layer, bool visible ); + /** + * Sets the currently selected \a node. + * + * If \a node is NULLPTR then all nodes will be deselected. + * + * \see currentNode() + * \since QGIS 3.40 + */ + void setCurrentNode( QgsLayerTreeNode *node ); + /** * Sets the currently selected \a layer. *