2

I am using Qt Designer 4.8.5 to develop a plugin (with QGIS 2.14.3-Essen).

I recently started to use QgsMapLayerComboBox widgets instead of the conventional Combo Box widget. However, I want to filter out a number of layers so that only a few are shown.

When I used the Combo box widget, here is the code:

root = QgsProject.instance().layerTreeRoot()
main_group = root.findGroup('Main group')
layers = []
for group in main_group.children():
    for layer in group.children():                
        layers.append(layer.layerName())
self.dockwidget.combo_box.clear()
self.dockwidget.combo_box.addItems(sorted(layers))

How can I translate the above code to work with QgsMapLayerComboBox? I have seen the QgsMapLayerComboBox.setExceptedLayerList() but not exactly sure how to create such a list.

2

Guess it was easier than I thought, I just had to create a list of QgsMapLayers from a group I want excluded in the QgsMapLayerComboBox:

root = QgsProject.instance().layerTreeRoot()
other_group = root.findGroup('Other group')
layers = []
for group in other_group.children():
    for layer in group.children():
        layerList = QgsMapLayerRegistry.instance().mapLayersByName(layer.layerName())[0]
        layers.append(layerList)
self.dockwidget.combo_box.setExceptedLayerList(layers)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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