Although this should seem to be very straightforward, I cannot find out how to see the number of layers I have currently selected in QGIS. For example, in the example below I want to see somewhere in the GUI the number 5, or be able to somehow retrieve this number:

Example of layer selection

Obviously, with only five layers selected, I can directly see how many layers I have selected. However, with a lot of layers, it can become an arduous task if I have to count each layer myself. I want to quickly compare the number of layers I have in two different sets of layers. Ideally, I would also be able to see the number of layers within a layer (sub)group (e.g. "Input [X]", where 'X' is the number of layers in the group).

Is there something I should change in the QGIS View settings? Or is there any other method to quickly retrieve this number?

  • this is a great question!
    – Taras
    Feb 4, 2023 at 8:29

2 Answers 2


You can use this script. It adds a toolbar as seen in the image and shows how many layers are selected.

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

tree_view = iface.layerTreeView()
c = len(tree_view.selectedLayers())

tb = iface.addToolBar("Selected layers")

def show_selected_layers_count():
    c = len(tree_view.selectedLayers())


enter image description here


To extend the great solution by @Kadir Şahbaz, you could do something like this to get a count per group:

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

tb = iface.addToolBar("Selected layers")

def show_selected_layers_count():
    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:
            count = len(selected_layers)
            count_label = QLabel(str(count) + '    ')
con = view.selectionModel().selectionChanged.connect(show_selected_layers_count)

# to disconnect the function from the signal

enter image description here

  • Great addition, thank you @Matt!
    – Sytze
    Jan 17, 2023 at 14:32
  • 1
    It currently works with 1st level groups. It will need some modification to work with sub-groups, let me know if you need me to update the function
    – Matt
    Jan 17, 2023 at 14:59
  • That would actually be very useful, so please, go ahead!
    – Sytze
    Jan 18, 2023 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.