[Qgis-user] layer groups with python plugin

Jürgen E. Fischer jef at norbit.de
Tue Mar 16 05:59:43 PDT 2010


Hi Cédric,

On Tue, 16. Mar 2010 at 12:26:07 +0100, Möri Cedric wrote:
> uri = QgsDataSourceURI()
> uri.setConnection(self.DBHOST, self.DBPORT, self.DBNAME, self.DBUSER, "")
>  uri.setDataSource("public","table_foo","wkb_geometry")
> vLayer = QgsVectorLayer(uri.uri(), "myLayer", "postgres")
> QgsMapLayerRegistry.instance().addMapLayer(vLayer)    
> 
> This works fine. My question is now, how may I add the layers in a group in
> the toc? In the layer list you may right click and then add a group and then
> you may add your layers in the group. That's what I like to do directly with
> my plugin. 

idx = qgis.utils.iface.legendInterface().addGroup( "group_foo" );
qgis.utils.iface.legendInterface().moveLayer( vLayer, idx );

where idx is the row index of the new group in the tree - so once the layer is
move to the group, you need to adjust the index.


Jürgen

-- 
Jürgen E. Fischer         norBIT GmbH               Tel. +49-4931-918175-20
Dipl.-Inf. (FH)           Rheinstraße 13            Fax. +49-4931-918175-50
Software Engineer         D-26506 Norden               http://www.norbit.de

-- 
norBIT Gesellschaft fuer Unternehmensberatung und Informationssysteme mbH
Rheinstrasse 13, 26506 Norden
GF: Jelto Buurman, HR: Amtsgericht Emden, HRB 5502




More information about the Qgis-user mailing list