17

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?

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

2 Answers 2

23

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())
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)

enter image description here

2
16

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()

## TOOLBAR
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 the function from the signal
#view.selectionModel().selectionChanged.disconnect(con)

enter image description here

3
  • 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.