[Qgis-developer] Reliable way to determine a groupIndex in the legend tree

Bernhard Ströbl bernhard.stroebl at jena.de
Tue Oct 8 06:32:32 PDT 2013


Hi Andreas,

this is how I do it:

def getGroupIndex(self,  groupName):
     '''Find the index for group groupName'''
     retValue = -1
     groups = self.iface.legendInterface().groups()

     for i in range(len(groups)):
         if groups[i] == groupName:
             retValue = i
             break

     return retValue

def moveToGroup(self, layer, groupName):
     '''move layer into a group'''

     grpIdx = self.getGroupIndex(groupName)

     if grpIdx == -1:
        # add the group
        grpIdx = self.iface.legendInterface().addGroup(groupName)

     self.iface.legendInterface().moveLayer(layer,  grpIdx)

Bernhard

Am 08.10.2013 15:17, schrieb Andreas Neumann:
> Hi,
>
> In my python script I would like to append a filtered Postgis layer into
> a group with a given name.
>
> This works fine as long as the user did not activate a group already
> interactively. If a group is already active, my method to find the
> groupIndex fails. If not group is active, the script works fine.
>
> Here is my method:
>
> ---------------
> #read the legend tree to find the index of the group defined in groupName
> groups = []
> for group in self.legend.groupLayerRelationship():
> groups.append(group[0])
> groupIndex = groups.index(groupName)
> if QGis.QGIS_VERSION[0:3] >= "1.9":
> groupIndex -= 1
>
> #move new layer to existing group
> self.legend.moveLayer(schulkreisLayer, groupIndex)
> --------------
>
> Is there a more reliable way to find the groupIndex if the user selected
> a group already?
>
> Maybe there is some interference with the setting "New layers should be
> added to chosen or current group" setting in "Settings" --> "Options"
> --> "Map & Legend" --> "Layerlegend"?
>
> Thanks,
> Andreas
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>



__________ Information from ESET Mail Security, version of virus signature database 8890 (20131008) __________

The message was checked by ESET Mail Security.
http://www.eset.com




More information about the Qgis-developer mailing list