[Qgis-user] Label Visibility

Charles Dixon-Paver charles at kartoza.com
Wed Apr 14 07:34:53 PDT 2021


If you want the layer dependant label visibility to be applied to the
entire layer, then indeed using an expression in the "Show label" rendering
option should do what you are asking. By the sounds of things though, you
should probably just be using a simple expression like:

if (is_layer_visible( 'toggle_layer_name' ), 1, 0)

Invert it to only display the labels when a certain layer is *not* visible
by simply switching the true/ false statement order, e.g. if
(is_layer_visible( 'toggle_layer_name' ),false,true)

If you are wanting the behaviour to be applied to a specific rule you can
specify a similar context within the rule using an expression like if
(is_layer_visible( 'toggle_layer_name' ),{expression when true}, false)

Note, however, that although the map canvas will automatically toggle the
display of the visible layers, it probably isn't going to automatically
update the label rendering... So even with these expressions working
properly you may have to refresh the map or pan a little to check that it's
working after changing the layer visibility. As I'm sure you can imagine,
labeling can be quite a computationally expensive resource so there are
likely very good reasons for this behaviour, although you can probably work
around it using some python code or similar. An example might be the
following:

def redrawWhenToggled(layerTreeNode):
    canvas = iface.mapCanvas()
    canvas.redrawAllLayers()

toggle_layer =
QgsProject.instance().layerTreeRoot().findLayer('toggle_layer_name_76f7c585_xxxxx')
toggle_layer.visibilityChanged.connect(redrawWhenToggled)

That should redraw the canvas each time you toggle the visibility on that
particular layer. Note that 'toggle_layer_name_76f7c585_xxxxx' is of course
the layerID. You can easily get the layer id by selecting/ highlighting the
item in the layers tab and using iface.activeLayer().id() in the python
console.

I would probably also use a layer id, rather than a name, in the
"is_layer_visible" expression, so that the expression continues to work
even when a layer is renamed or duplicated etc.

On Tue, 13 Apr 2021 at 23:27, Springfield Harrison <stellargps at gmail.com>
wrote:

> Hello Folks, back again!
>
> I'm trying to have labels display only when that "layer" is turned on
> (visible).  BUT my layers are actually rule based symbology and apply only
> to selected records in the layer (as per the rules), (sub-layers?).
> Under the Rendering tab for labels I find:
>
> *With data-defined expressions in **Show label** and **Always Show** you
> can fine tune which labels should be rendered.*
>
> but there is no explanation of how to implement this feature.
>
> I would expect that something like this may be in the right direction:
>
> *if (is_layer_visible( @layer_name ) = 0,  ("Tree_Tag"  ||  ' - ' || ' ('
> ||   "More"   || ') ') , '*')*
>
> Any thoughts on how to selectively make labels visible would be great.
>
> -----
> Cheers, Spring
>
>
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210414/ec74c611/attachment-0001.html>


More information about the Qgis-user mailing list