[QGIS-Developer] Adding Layer tree embedded widgets programmatically

Ricardo Filipe Soares Garcia da ricardo.garcia.silva at gmail.com
Wed Feb 13 06:55:07 PST 2019


Hi Richard

You can add widgets to a specific layer if you know the widget provider's
id. It is simply a matter of setting up the relevant custom properties.

Take a look at the code in the PR you linked, more specifically here:

https://github.com/qgis/QGIS/pull/3170/files#diff-0c8d41d794f1b84a1291aa29bb39392aR114


For example, the standard widget that provides a transparency slider has an
id of "transparency". Lets imagine I select the current layer in the canvas
and then want to make this widgets appear in its legend:

```python
layer = iface.activeLayer()
layer.setCustomProperty("embeddedWidgets/count", 1)
layer.setCustomProperty("embeddedWidgets/0/id", "transparency")
iface.layerTreeView().refreshLayerSymbology(layer.id())
```

This code assumes that the layer doesn't currently have any embedded
widgets.


P.S. - Now that I've tried it, it seems that the transparency slider works
differently for raster and vector layers.


Richard Duivenvoorde <rdmailings at duif.net> escreveu no dia quarta,
13/02/2019 à(s) 11:38:

> Hi Devs,
>
> Thinking about creating a small plugin which would add a 'Layer tree
> embedded widget' to a layer in the legend, I hit:
>
> https://github.com/qgis/QGIS/pull/3170
>
> with a nice example on how to create such a widget (with 2 minor tweaks
> see below).
>
> But my question is if it is possible (in the python plugin) to attach
> such widget to a specific layer? Now it is working but you can only
> activate it via the layerproperties dialog.
>
> I could not find a example or hint for it.
>
> Any pointers appreciated.
>
> Regards,
>
> Richard Duivenvoorde
>
> PS
>
> To make Martins example https://github.com/qgis/QGIS/pull/3170) work in
> QGIS3:
>
> from PyQt4.QtGui import QComboBox
> becomes
> from qgis.PyQt.QtWidgets import QComboBox
>
> QgsLayerTreeEmbeddedWidgetRegistry.instance().addProvider(provider)
> becomes
> QgsGui.layerTreeEmbeddedWidgetRegistry().addProvider(provider)
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer



-- 
___________________________ ___ __
Ricardo Garcia Silva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20190213/89408fb9/attachment.html>


More information about the QGIS-Developer mailing list