1

I would like to filter the layerlist in the "Snapping and Digitizing Options"-Dock so that only layers are shown which are selected in the TOC of the QGIS-Layer-Dock.

At the moment I use a workaround which searches the UI components like

self.iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options')

This solution seems a bit hacky to me so I like to ask if someone has a better idea to filter the layerlist in the Snapping-Dock?

enter image description here

The goal is to filter 5 or 10 layers at the same time, not only one like it can be done in the filter textfield at the bottom of the snapping-dock. Here is my code:

    def filter_snapping_list(self, reset):
    global filter_List
    if self.dlg==None:
        self.dlg = self.iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options')
    snapping_mode = self.dlg.findChildren(QToolButton)[2].text()   

    if snapping_mode in ["Advanced", "Erweiterte Konfiguration"]: 

        self.treeview = self.iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options').findChildren(QTreeView)[0]
        self.treecount = self.iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options').findChildren(QTreeView)[0].model().rowCount()

        for i in range(self.treecount):
            if reset == False:
                if self.iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options').findChildren(QTreeView)[0].model().index(i,0).data() in filter_List:
                    self.iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options').findChildren(QTreeView)[0].setRowHidden(i, QModelIndex(), False)
                else:
                    self.iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options').findChildren(QTreeView)[0].setRowHidden(i, QModelIndex(), True)
            else:
                self.iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options').findChildren(QTreeView)[0].setRowHidden(i, QModelIndex(), False)

    else:
        self.iface.messageBar().pushMessage("Error", "Set Snapping-Mode to Advanced!", level=Qgis.Critical, duration=3)



def get_filter_layers(self):
    selectedLayers = self.iface.layerTreeView().selectedLayers()
    if len(selectedLayers)<1:
        self.iface.messageBar().pushMessage("Error", u"No Layers chosen in the TOC !", level=Qgis.Critical, duration=3)
    else:
        global filter_List
        filter_List=[]
        for i in selectedLayers:
            filter_List.append( i.name() )
1

Use this code for update automatically the snap list using only selected layers:

snapDock = iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options')
treeView = snapDock.findChildren(QTreeView)[0]
root = QgsProject.instance().layerTreeRoot()
model = iface.layerTreeView().layerTreeModel()
view = iface.layerTreeView()
canvas = iface.mapCanvas()

def ShowSelected(item):
    #Hide All
    for layer in canvas.layers():
        node = root.findLayer( layer.id())
        index = model.node2index( node )
        treeView.setRowHidden(index.row(), QModelIndex(), True)
    #Show Selected Only
    for node in view.selectedNodes ():
        index = model.node2index( node )
        treeView.setRowHidden(index.row(), QModelIndex(), False)

view.selectionModel().selectionChanged.connect(ShowSelected)

Note: currentLayerChanged signal not worked properly in this purpose

enter image description here

I hope it helps you

  • nice Idea to filter automatically. still there seems no better way to access the layerlist than access it by calling gui-elements like snapDock.findChildren(QTreeView)[0] – markgraeflerland Feb 22 at 14:00

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.