[Qgis-developer] List layer from Legend

Martin Dobias wonder.sk at gmail.com
Tue Jan 19 06:56:16 EST 2010


Hi Luiz

On Mon, Jan 18, 2010 at 9:53 PM, Luiz Motta <motta.luiz at gmail.com> wrote:
> Hi all,
>
> I am making the python plugin and need show layers in QTreeWidget with same
> order show in Legend/Layer.
>
> The list of layers by :
> layerRegistry = core.QgsMapLayerRegistry.instance()
> it is not same order by Legend.

Right, the layers in map layer registry have arbitrary ordering.


> I have success get list of layer's name by find the QTreeWidget with name
> 'Legend':
> ### getDocWidgetLegend
> def getDocWidgetLegend():
>  widget = None
>  lst = qgis.utils.iface.mainWindow().children()
> [...]

The problem with this approach is that the implementation details
could change in future releases without any warning and will lead to
broken code.


> The problem is when the have same layer's name in Legend, i don't know how
> take exact source.

You could use item's tooltip() method - it returns the same string as
layer.publicSource() so it might be a reliable identification of the
layer. But remember: that's also an implementation detail that can be
changed.


> The other method is read the entries in project:
> project = core.QgsProject.instance()

Unfortunately project doesn't always reflect the current state, so you
might work with outdated entries.

There might be a better solution, however it will require you to do a
bit of c++ coding. From v1.4 there's QgsLegendInterface class which
allows plugin developers to work with the legend widget in a more
convenient way. Currently the interface doesn't offer much, but I
would be happy to see any patches that add methods that allow listing
of layers etc.

Martin


More information about the Qgis-developer mailing list