In PyQGIS we can obtain the active (currently selected) layer with
>>> iface.activeLayer()
<QgsRasterLayer: '007' (gdal)>
With a group selected this returns None
(?).
QgsInterface
does not seem to have a method to obtain the active group, is there any workaround to find out the active group from PyQGIS?
Best Answer
The layer tree is a
QTreeView
object where you can access any selected row via itsselectionModel
.The
activeGroup
function will work equivalent toactiveLayer
. It returns aQgsLayerTreeGroup
object if a group is selected otherwise it returnsNone
.