1

I can't stop getting this error message when I try to run the following code in Qgis 2.14.10 :

from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtCore import QSettings
from PyQt4.QtCore import QSettings
from qgis.core import QgsVectorLayer, QgsDataSourceURI

uri = QgsDataSourceURI()
uri.setConnection("localhost", "5432", "name of my database", "my user name", "my password")
uri.setDataSource("database schema", "table name", "geom")
couche = "PM"
layer = QgsVectorLayer(uri.uri(), couche, "postgres")
if not layer.isValid:
    print ('Chargement de la couche: echec !')
else:
    QgsMapLayerRegistry.instance().addMapLayers(layer)

TypeError: QgsMapLayerRegistry.addMapLayers(list-of-QgsMapLayer, bool addToLegend=True): argument 1 has unexpected type 'QgsVectorLayer'

Could someone tell me what I'm doing wrong please ? :)

1 Answer 1

2

You are trying to add a single layer using a function designed for adding multiple layers.

Either use

QgsMapLayerRegistry.instance().addMapLayer(layer)

or put the single layer into a (single element) list

QgsMapLayerRegistry.instance().addMapLayers([layer])
2
  • 1
    Damn it I'm sorry ! I went through all the lines of my script again and again but I couldn't see that tricky "s" ! :) Thanks !!!
    – wanderzen
    Dec 17, 2020 at 15:13
  • 1
    Happens to everyone, at least two times. Dec 17, 2020 at 16:11

Your Answer

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

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