2

I'm filtering the layers in a QgsMapLayerComboBox and in addition to filtering according to geometry, I want to remove the layers that are disabled? Is it possible? How do I do it? I'm using the following code to filter according to geometry.

self.CMBNODES.setFilters(QgsMapLayerProxyModel.PointLayer)
self.CMBPIPES.setFilters(QgsMapLayerProxyModel.LineLayer)
self.CMBMDE.setFilters(QgsMapLayerProxyModel.RasterLayer )
self.CMBCURVAS.setFilters(QgsMapLayerProxyModel.NoGeometry)
3
  • By 'disabled', do you mean their visibility is checked off?
    – Matt
    Commented Aug 11 at 21:44
  • Yes, I can get a list of the layers that are in QGIS and a list of which are active/enabled. layers_enableds = self.iface.mapCanvas().layers() <><><> layers = QgsProject.instance().mapLayers().values() Commented Aug 11 at 23:18
  • QgsMapLayerComboBox has a setExceptedLayerList() method which you can pass a list of layers which you want to exclude from being shown in the combo box. api.qgis.org/api/…
    – Ben W
    Commented Aug 12 at 0:49

1 Answer 1

6

Use the setExceptedLayerList() method of QgsMapLayerComboBox. You can pass this method a list of layers which want to exclude from being shown in the combo box. You will probably want to run this logic in a method which is also connected to the QgsMapCanvas.layersChanged signal.

Simple example below:

class TestDialog(QDialog):
    def __init__(self):
        super(TestDialog, self).__init__()
        self.canvas = iface.mapCanvas()
        self.setGeometry(300, 300, 500, 200)
        self.mlcb = QgsMapLayerComboBox(self)
        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.mlcb)
        self.filter_layers()
        self.canvas.layersChanged.connect(self.filter_layers)
        
    def filter_layers(self):
        checked_layers = self.canvas.layers()
        excluded_layers = [l for l in QgsProject.instance().mapLayers().values() if not l in checked_layers]
        self.mlcb.setExceptedLayerList(excluded_layers)
        
dlg = TestDialog()
dlg.show()

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.