TransWikia.com

Remove layer indicator in QGIS

Geographic Information Systems Asked by TrsBar on September 26, 2020

I would like to be able to change or remove the tooltip when the mouse is over a layer in TOC.
I’ve seen in source code that i must to use the QgsLayerTreeViewIndicator class to perform it, but it doesn’t work (crash).

layerNodes = QgsProject.instance().layerTreeRoot().findLayers()
ltw = self.iface.layerTreeView()
for node in layerNodes :
    if (node.layer() is not None) :
        indicator = QgsLayerTreeViewIndicator(node.layer())
        ltw.addIndicator(node, indicator)

If is it possible to change the default publicSource method to display a better information, i take as solution too.

One Answer

The best solution to customize the TOC it's to create a LayerTreeModel class. Then in this class, overload the default data method. It's look like this :

class LayerTreeModel(QgsLayerTreeModel):
    
    def __init__(self, parent=None):
        super().__init__(QgsProject.instance().layerTreeRoot(), parent)
        self.setFlags(iface.layerTreeView().layerTreeModel().flags())

    def data(self, index, role=Qt.DisplayRole):
        if not index.isValid():
            return None
        
        node = self.index2node(index)
       
        if (role == Qt.ToolTipRole and QgsLayerTree.isLayer(node)) :
            return ''
       
        return super().data(index, role)

Main Class :

self.originalLayerTreeModel = self.iface.layerTreeView().model()
self.originalLayerTreeModel.blockSignals(True)
self.layerTreeModel = LayerTreeModel()
self.iface.layerTreeView().setModel(self.layerTreeModel)

Correct answer by TrsBar on September 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP