TransWikia.com

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

Geographic Information Systems Asked on February 1, 2021

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 ? 🙂

One Answer

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])

Answered by bugmenot123 on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP