0

I successfully find coding for calling data from postgresql into QGIS layer panel.

enter image description here

The coding as below;

import qgis
root = qgis.core.QgsProject.instance().layerTreeRoot()
groupName = root.addGroup('Cerun')

uri = QgsDataSourceURI() uri.setConnection("localhost", "5432", "amsdb", "postgres", "postgres")

uri.setDataSource('public', 'slope', 'geom', '', 'name') layer = qgis.core.QgsVectorLayer(uri.uri(), 'slope', 'postgres') print layer.isValid() QgsMapLayerRegistry.instance().addMapLayer(layer,False) groupName.addLayer(layer)

uri.setDataSource("public", "slopeinfo", None) layer = QgsVectorLayer(uri.uri(), "slopeinfo", "postgres") print layer.isValid() QgsMapLayerRegistry.instance().addMapLayer(layer,False) groupName.addLayer(layer)

uri.setDataSource("public", "services_facilities", None) layer = QgsVectorLayer(uri.uri(), "services_facilities", "postgres") print layer.isValid() QgsMapLayerRegistry.instance().addMapLayer(layer,False) groupName.addLayer(layer)

uri.setDataSource("public", "sign_of_distress", None) layer = QgsVectorLayer(uri.uri(), "sign_of_distress", "postgres") print layer.isValid() QgsMapLayerRegistry.instance().addMapLayer(layer,False) groupName.addLayer(layer)

uri.setDataSource("public", "surface_subsurface", None) layer = QgsVectorLayer(uri.uri(), "surface_subsurface", "postgres") print layer.isValid() QgsMapLayerRegistry.instance().addMapLayer(layer,False) groupName.addLayer(layer)

uri.setDataSource("public", "engineering_judgement", None) layer = QgsVectorLayer(uri.uri(), "engineering_judgement", "postgres") print layer.isValid() QgsMapLayerRegistry.instance().addMapLayer(layer,False) groupName.addLayer(layer)

uri.setDataSource("public", "structure", None) layer = QgsVectorLayer(uri.uri(), "structure", "postgres") print layer.isValid() QgsMapLayerRegistry.instance().addMapLayer(layer,False) groupName.addLayer(layer)

enter code here

The problem is I want to adding all the table layer under Group>SubGroup>Layer just like picture below. All the Group>SubGroup>Layer already in Layer Panel. Need to find coding to adding the table under Group>SubGroup>Layer.

enter image description here

Can anyone help?

Khadijah Nas
  • 129
  • 7

2 Answers2

0

If your sub group name is unique, you can use

project = QgsMapLayerRegistry.instance()
# project = QgsProject.instance() # If QGIS 3.0
root = project.layerTreeRoot()
mygroup = root.findGroup("CERUN")
project.addMapLayer(layer, False)
mygroup.addLayer(layer)

Or if same name for multiple subgroups, take the parent and then select it child from subgroup name

project = QgsMapLayerRegistry.instance()
# project = QgsProject.instance() # If QGIS 3.0
root = project.layerTreeRoot()
mygroup = root.findGroup("KATEGORI")
mysubgroup = mygroup.findGroup("CERUN")
project.addMapLayer(layer, False)
mysubgroup.addLayer(layer)

You may need to create the subgroup (if not here) with

project = QgsMapLayerRegistry.instance()
# project = QgsProject.instance() # If QGIS 3.0
root = project.layerTreeRoot()
mygroup = root.findGroup("KATEGORI")
mysubgroup = root.addGroup("CERUN")
project.addMapLayer(layer, False)
mysubgroup.addLayer(layer)
ThomasG77
  • 30,725
  • 1
  • 53
  • 93
0

Thanks so much Thomas, already working with the script below;

import qgis
root = qgis.core.QgsProject.instance().layerTreeRoot()
groupName = root.findGroup('KATEGORI')
subgroupName = groupName.findGroup("CERUN")
subgroupName = groupName.findGroup("Slope") 

Its run smoothly when run it manually on Python Console window.

The new prob comes when i add all these new codes in plugin python script. After run the plugin, the result turns no tables has been added on the QGIS layer panel.

Can anyone familiar with QGIS customize plugin help me?

Khadijah Nas
  • 129
  • 7
  • Not sure but why do you use subgroupName = groupName.findGroup("Slope") here? "Slope" is not a group but a layer according to your screenshot. I would expect use of findLayer e.g https://qgis.org/pyqgis/master/core/QgsLayerTreeGroup.html#qgis.core.QgsLayerTreeGroup.findLayer/QgsLayerTreeGroup.html#qgis.core.QgsLayerTreeGroup.findLayer – ThomasG77 Mar 11 '21 at 10:22