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

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
nmokht97
  • 143
  • 8
  • 1
    You say that you "have the same problem" but the same problem as what? – PolyGeo Jun 19 '20 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 '20 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 '20 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 '20 at 11:51

2 Answers2

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)
Vincent Bré
  • 4,090
  • 7
  • 23
  • 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 '20 at 09:01
  • Do you have an error message? – Vincent Bré Jun 19 '20 at 09:24
  • no error message – nmokht97 Jun 19 '20 at 09: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 '20 at 09:28
  • exec(open('C:/Users/NASSIM~1.MOK/AppData/Local/Temp/tmpmv2m58vg.py'.encode('utf-8')).read())

    – nmokht97 Jun 19 '20 at 09:44
  • Do you try outside a function? – Vincent Bré Jun 19 '20 at 09:53
  • it's exactly by using it outside of the function that it makes me bug QGIS. If you have any way to check if connexion is etablished I can maybe debug my problem. – nmokht97 Jun 19 '20 at 09:56
  • For this part I can refer you to the following link https://gis.stackexchange.com/questions/350457/verify-postgis-connection-username-and-password – Vincent Bré Jun 19 '20 at 10:07
  • Thank you Vincent – nmokht97 Jun 19 '20 at 11:48
  • Vincent, when I type layer in the console it gives me, 'code'(layer <QgsMapLayer: 'BPE' (Invalid)>) and QGIS still bugs and need to be restarted. do you know what is realy the error? – nmokht97 Jun 22 '20 at 08:52
  • You have this error when you execute the code written in your post? Check the name of your table, schema and geometry column – Vincent Bré Jun 22 '20 at 09:20
  • 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 xhene i'm using Pyqgis it give me "unusing layer". – nmokht97 Jun 22 '20 at 09:43
  • I'm out of ideas, sorry – Vincent Bré Jun 22 '20 at 09:47
  • 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 '20 at 08:00
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

nmokht97
  • 143
  • 8
  • 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 '20 at 07:25