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.5k29 gold badges115 silver badges350 bronze badges
asked Jun 19, 2020 at 8:00
4
  • 1
    You say that you "have the same problem" but the same problem as what? Commented Jun 19, 2020 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 Commented Jun 19, 2020 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 Commented Jun 19, 2020 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. Commented Jun 19, 2020 at 11:51

2 Answers 2

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)
answered Jun 19, 2020 at 8:20
14
  • 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 Commented Jun 19, 2020 at 9:01
  • Do you have an error message? Commented Jun 19, 2020 at 9:24
  • no error message Commented Jun 19, 2020 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 Commented Jun 19, 2020 at 9:28
  • >>>exec(open('C:/Users/NASSIM~1.MOK/AppData/Local/Temp/tmpmv2m58vg.py'.encode('utf-8')).read()) Commented Jun 19, 2020 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

answered Jun 22, 2020 at 15:17
1
  • I fixed my problem, I juste changed in 'code'layer = QgsVectorLayer(uriPG.uri(), "BPE","postgres") , I don't use my own user here. Commented Jun 23, 2020 at 7:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.