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?

Taras
  • 32,823
  • 4
  • 66
  • 137
Sytze
  • 731
  • 6
  • 18

2 Answers2

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

Taras
  • 32,823
  • 4
  • 66
  • 137
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
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

Taras
  • 32,823
  • 4
  • 66
  • 137
Matt
  • 16,843
  • 3
  • 21
  • 52