[Qgis-developer] Python: how to find all children of a group?

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Nov 23 10:17:25 EST 2011


On Wed, Nov 23, 2011 at 2:53 PM, Andreas Neumann <a.neumann at carto.net> wrote:
> Hi,
>
> I am trying to get a list of layers that are the children of a group with a
> certain group index (or even better by layer name).
>
> I can't figure out how to do this properly with the QgsLegendInterface
> object.
>
> Does one have a piece of code how to accomplish this?

 Well all have a piece of code that does that :) Qgis is open source,
so we've all got the source...

 And we have the documentation. I had a look at the qgis api docs:

http://www.qgis.org/api/classQgsLegendInterface.html

 - all the 'children' methods that you maybe tried are Qt things, and
so irrelevant and not listed, But that groupLayerRelationship method
looks interesting:

>>> l.groupLayerRelationship()
[[PyQt4.QtCore.QString(u''),
[PyQt4.QtCore.QString(u'NER_adm020111123150216751')]],
[PyQt4.QtCore.QString(u'group'),
[PyQt4.QtCore.QString(u'NER_adm320111123150216816'),
PyQt4.QtCore.QString(u'NER_adm220111123150216800'),
PyQt4.QtCore.QString(u'NER_adm120111123150216790')]],
[PyQt4.QtCore.QString(u'foobar'), []]]


Seems to be a nested list of layer names (as QString objects). If you
can unravel the output above, it starts with an empty root string,
then I've got a layer, a group with three layers, and then and empty
layer. You'll soon grok it.

 So just find the element of the group you are interested in and do a
depth-first or breadth first search to whatever depth you want.
Simples!

Barry


More information about the Qgis-developer mailing list