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

Andreas Neumann a.neumann at carto.net
Wed Nov 23 11:50:05 EST 2011


 Hi Marco,

 With Google I came across your project's code already.

 In my opinion it should be possible to easily get a layer and group ref 
 by id and from there traverse to parent/child/siblings - just like in a 
 webpage DOM.

 Maybe something we should sponsor next year ;-)?

 Andreas

 On Wed, 23 Nov 2011 16:41:19 +0100, Marco Bernasocchi wrote:
> Hi Andreas, in my multiview plugin there is sth similar, have a look.
>
> ciao
> 
> http://hub.qgis.org/projects/multiview/repository/revisions/master/entry/temporalrasterloaderdialog.py
>
>
>
> On 11/23/2011 04:29 PM, Andreas Neumann wrote:
>> Thanks Barry,
>>
>> I figured the method "groupLayerRelationship" and saw that it 
>> contains a
>> list of childLayers.
>>
>> I am trying to figure it out. It seems a bit complex to use for me. 
>> In
>> web development I do a lot of tree traversals in the DOM and seems 
>> much
>> easier to me ...
>>
>> But I also read that the QgsLegendInterface API grew over time and 
>> needs
>> some overhauling ...
>>
>> Thanks for the pointer!
>>
>> Andreas
>>
>> On Wed, 23 Nov 2011 15:17:25 +0000, Barry Rowlingson wrote:
>>> 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
>>

-- 
 --
 Andreas Neumann
 Böschacherstrasse 10A
 8624 Grüt (Gossau ZH)
 Switzerland


More information about the Qgis-developer mailing list