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

Andreas Neumann a.neumann at carto.net
Tue Oct 8 06:17:40 PDT 2013


 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


More information about the Qgis-developer mailing list