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

Xavier Barnada xbarnada at gisce.net
Wed Oct 19 04:57:13 PDT 2016


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?

Best reggards
Xavier Barnada
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20161019/f8139484/attachment.html>


More information about the Qgis-developer mailing list