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 <[email protected]>: > Hi Xavier > > On Wed, Oct 19, 2016 at 7:57 PM, Xavier Barnada <[email protected]> > 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 > > [email protected] > > List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer > > Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer >
_______________________________________________ Qgis-developer mailing list [email protected] List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
