2

I have the same problem, bellow my code if some one can help me please.

from qgis.core import *
from qgis.core import QgsProject
from PyQt5.QtCore import QFileInfo
from qgis.core import QgsVectorLayer, QgsDataSourceUri

def run_script(iface):
    uri = QgsDataSourceUri()
    uri.setConnection("localhost", "5432", "Base_test", "user", "****")
    uri.setDataSource("public", "mytable", "geom","")

    vlayer = QgsVectorLayer(uri.uri(), "mytable", "user")
    QgsMapLayerRegistry.instance().addMapLayers([layer])

When I run this code I haven't un error but i see nothing on my QGIS interface.

I'm using, QGIS 3.12.


from qgis.core import *
from qgis.core import QgsProject
from PyQt5.QtCore import QFileInfo
from qgis.core import QgsVectorLayer, QgsDataSourceUri
from qgis.utils import *
import processing

def run_script(iface):
    uri = QgsDataSourceUri()
    uri.setConnection("localhost", "5432", "Base_test", "Nassim", "*****")
    uri.setDataSource("public", "BPE", "geom")

    layer = QgsVectorLayer(uri.uri(), "BPE", "Nassim")
    QgsProject.instance().addMapLayer(layer)

This is my code whith the solution given by @Vincent Bré, but it still doesn't work

  • 1
    You say that you "have the same problem" but the same problem as what? – PolyGeo Jun 19 at 10:04
  • Did you run run_script(iface)? It's because all the code you show us is only declaring the function but you need to call it to work – ThomasG77 Jun 19 at 11:07
  • Sorry PolyGeo, I juste made un copy past from an other post, my problème it's: i can't display my layer on QGIS interface whene I load it from Postgis DB – nmokht97 Jun 19 at 11:45
  • I have a new problem now, this solution seems working but it bug my QGIS app and i need to restart it. – nmokht97 Jun 19 at 11:51
3

You should use QgsProject instead of QgsMapLayerRegistry.

To add a single layer, you can use the addMapLayer method.

You can use the following code:

uriPG = QgsDataSourceUri()
uriPG.setConnection("localhost", "5432", "database", "user", "password")
uriPG.setDataSource("schema", "name_table", "geometry_column")
layer = QgsVectorLayer(uriPG.uri(), "name_table_in_qgis", "user")
QgsProject.instance().addMapLayer(layer)
| improve this answer | |
  • Thank you for your answser, it stil not working for me. I don't know why besacuse it's excatlty you code what I put whith the good parameters .in addition, it makes my QGIS buggy and you have to relaunch it – nmokht97 Jun 19 at 9:01
  • Do you have an error message? – Vincent Bré Jun 19 at 9:24
  • no error message – nmokht97 Jun 19 at 9:26
  • My code works. You run it from the Python console in QGIS? Make sure you check the name of your table, the geometry column – Vincent Bré Jun 19 at 9:28
  • >>>exec(open('C:/Users/NASSIM~1.MOK/AppData/Local/Temp/tmpmv2m58vg.py'.encode('utf-8')).read()) – nmokht97 Jun 19 at 9:44
1

I made a modification on layer = QgsVectorLayer(uri.uri(), "BPE", "") removing the user in this function, what stope the bug, I'm sure of my parameters because when I load the same table frome postgis using the interface all is ok. but whene I'm using Pyqgis it give me "unusing layer"

uriPG = QgsDataSourceUri()
uriPG.setConnection("localhost", "5432", "Base_test", "Nassim", "******")
uriPG.setDataSource("public", "CABLES", "geom")
layer = QgsVectorLayer(uriPG.uri(), "BPE",)
if not layer.isValid():
    print("Layer %s did not load" %layer.name())    

QgsProject.instance().addMapLayer(layer)

bellow the picture of my result in the console and QGIS interface

it gives " an using layer"

enter image description here

| improve this answer | |
  • I fixed my problem, I juste changed in 'code'layer = QgsVectorLayer(uriPG.uri(), "BPE","postgres") , I don't use my own user here. – nmokht97 Jun 23 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.