Hi, Thank you Bernhard. This works better than my code with the legend.groupLayerRelationship()
We should add this to the Python Cookbook (http://www.qgis.org/en/docs/pyqgis_developer_cookbook/) - Do you mind? Or should I do a pull request? Do you have a clever strategy to make this same code available in multiple QGIS plugins? But really this should go into the core QGIS API. It is annoying taking care of such things. Andreas Am 08.10.2013 13:32, schrieb Bernhard Ströbl: > 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 >> [email protected] >> 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 > > > _______________________________________________ > Qgis-developer mailing list > [email protected] > http://lists.osgeo.org/mailman/listinfo/qgis-developer _______________________________________________ Qgis-developer mailing list [email protected] http://lists.osgeo.org/mailman/listinfo/qgis-developer
