[Qgis-developer] Add a layer to a group of layers

Xavier Barnada xbarnada at gisce.net
Wed Oct 19 05:57:56 PDT 2016


As you said I solved it using addMapLayer with a False parameter to
indicate to not add on the legend

Thank you
Xavier Barnada

2016-10-19 14:07 GMT+02:00 Martin Dobias <wonder.sk at gmail.com>:

> Hi Xavier
>
> On Wed, Oct 19, 2016 at 7:57 PM, Xavier Barnada <xbarnada at gisce.net>
> wrote:
> > Hi,
> >
> > I'm developing a plugin in Python and I want to add a vector layer to a
> > layer group. I'm trying to do it using this:
> >
> >
> > import qgis
> > root = qgis.core.QgsProject.instance().layerTreeRoot()
> > test_group = root.addGroup('Test')
> >
> > uri = qgis.core.QgsDataSourceURI()
> > uri.setConnection('172.26.0.106', '5432','gis', 'gis','gis')
> > uri.setDataSource('public', 'test_table', 'geom', '', 'name')
> > layer = qgis.core.QgsVectorLayer(uri.uri(), 'Test', 'postgres')
> > print layer.isValid()
> > test_group.addLayer(layer)
> >
> >
> > The layer is valid but it don't appear on the layer group
> >
> > Does it need any extra action?
>
> Yes, one needs to add the layer to the map layer registry before
> adding it to the layer tree. Just insert this
>
> QgsMapLayerRegistry.instance().addMapLayer(layer)
>
> before the last line with addLayer() call.
>
> Cheers
> Martin
>
>
> >
> > Best reggards
> > Xavier Barnada
> >
> > _______________________________________________
> > Qgis-developer mailing list
> > Qgis-developer at lists.osgeo.org
> > List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> > Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20161019/cc0d8361/attachment.html>


More information about the Qgis-developer mailing list