Bug report #15844
Layertree node texts in legend are not repainted after (name) change?
|Affected QGIS version:||master||Regression?:||No|
|Operating System:||Easy fix?:||No|
|Pull Request or Patch supplied:||No||Resolution:|
|Crashes QGIS or corrupts data:||No||Copied to github as #:||23762|
Having a layer of which the data is periodically updated, I wanted to show the update time in the layer in the legend.
It is easy to do (and to test in Python console):
treenode = QgsProject.instance().layerTreeRoot().insertGroup(0, 'Test node') treenode.setName('Renamed Test Node')
Result: the Legend is NOT updated UNTILL you move your mouse over the legend. But moving your mouse over the legend THEN it is.
Apparently some kind of repaint event is missing somewhere?
I could not find a handle to the legend (in python) to do a repaint/refresh of the widget.
Tested in 2.18 and master/3
(assigning to Martin??, as he is the layertreenodeman :-) but feel free to move it around)
#3 Updated by Martin Dobias over 4 years ago
There are some missing signals, that is why the updates do not work automatically. For 2.x one could use this as a workaround:
model = iface.layerTreeView().model() index = model.node2index(treenode) model.setData(index, 'new name')
The issue is there also when renaming raster layers (for unknown reasons only vector layers used to have name changed signal).
For master/3.0 I will make a fix, and possibly port it to 2.18.x
#5 Updated by Richard Duivenvoorde over 4 years ago
@Martin: Tested and Working in 3.0. thanks!
Looking at this line
I think the creation of a layername+featurecount is also done there, but I'm not sure if this also fixes this issue:
which is working with "layer.setSubsetString(subsetString)"...
which also should update the 'name' of the layer because it contains the featurecount...
What do you think, is that also fixed now? I cannot test because TimeManager is not working yet in QGIS3...
Do you think we could/should cherrypick this to 2.16 or 2.18?
#7 Updated by Richard Duivenvoorde over 4 years ago
@Martin I tested in 2.18: OK now
but instead of creating a 'setName' signal, shouldn't there be a more generic signal? To update the layertree labels?
I reckon more plugins/functions will start updating names/featurecounts/whateverfuturebrings and it would be good to have a 'updatelegend' signal or so? So it can be used as some "'repaint/reconstruct' the legend again based on the model"?
Or do you think that is overkill?
#8 Updated by Martin Dobias over 4 years ago
The layer tree does not know anything about legend, so requests for legend update need to be handled elsewhere. Layer tree only cares about groups and layers.
There is QgsLayerTreeModel::refreshLayerLegend(nodeLayer) method to update legend of a layer in a particular tree model. It is already called automatically on various occasions.