[Qgis-developer] Add a layer to a group of layers
Martin Dobias
wonder.sk at gmail.com
Wed Oct 19 05:07:08 PDT 2016
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
More information about the Qgis-developer
mailing list