When working with numerous layers in QGIS, it is often necessary to select multiple layers simultaneously for operations. However, with a large number of layers, it becomes difficult to quickly discern exactly how many are selected—don't worry, you can easily solve this by writing a small tool with PyQGIS!

Today, we share two methods to display the count of selected layers in real-time. These methods are straightforward to implement, with clear code, and can be tried by those in need.

Method 1: Real-Time Display of Total Selected Layers

This is currently the most popular approach in the community, suitable for quickly viewing the total number of selected layers. The specific code implementation is as follows:

from qgis.PyQt.QtWidgets import QLabel

label = QLabel("Selected Layers Count:")
count = QLabel()

tree_view = iface.layerTreeView()
c = len(tree_view.selectedLayers())
count.setText(str(c))

tb = iface.addToolBar("Selected layers")
tb.addWidget(label)
tb.addWidget(count)

def show_selected_layers_count():
    c = len(tree_view.selectedLayers())
    count.setText(str(c))

tree_view.selectionModel().selectionChanged.connect(show_selected_layers_count)

Run the above code directly in QGIS, and the effect is as shown:

Implementation Principle:

• Obtain the layer tree via iface.layerTreeView();

• Initially calculate and display the selected count;

• Achieve real-time updates using the selectionChanged signal.

Method 2: Count Selected Layers by Group

The above method is simple and quick but has a minor limitation: it does not support layer groups. Here is an enhanced version.

Code:

from qgis.PyQt.QtWidgets import QLabel
from qgis.core import QgsProject

p = QgsProject.instance()
root = p.layerTreeRoot()
view = iface.layerTreeView()

tb = iface.addToolBar("Selected layers")

def show_selected_layers_count():
    tb.clear()
    tb.addWidget(QLabel('Selected layers per group:      '))
    for child in root.children():
        if root.isGroup(child):
            label = QLabel(f'{child.name()}: ')
            selected_layers = []
            for selectedLayer in view.selectedNodes():
                if selectedLayer.parent() == child:
                    selected_layers.append(selectedLayer)
            count = len(selected_layers)
            count_label = QLabel(str(count) + '    ')
            tb.addWidget(label)
            tb.addWidget(count_label)

con = view.selectionModel().selectionChanged.connect(show_selected_layers_count)
# To disconnect, use:
# view.selectionModel().selectionChanged.disconnect(con)

The running result is as follows:

Summary

Finally, we recommend saving the script as a .py file and placing it in QGIS's startup script directory (e.g., ~/.qgis3/python). This will automatically load the script every time QGIS starts. To further extend functionality, you can optimize Method 2—for example, by adding support for recursively counting sub-groups or customizing the display style. Additionally, you can try adjusting styles such as adding background colors or borders to the labels to make the interface more intuitive and visually appealing.