3

I am trying to migrate a plugin from QGIS 2 to QGIS 3. This function doesn't work with QGIS 3 :

def getLayersByKeyWord(keyword):
    #retourne une liste de layer
    res=[]
    layerMap = QgsMapLayerRegistry.instance().mapLayers()
    for name, layer in layerMap.iteritems():
        if layer.keywordList() == keyword:
            res.append(layer)
    if len(res)>0:
        return res
    else:
        return None

I tried to replace QgsMapLayerRegistry with QgsProject but it is still not working on iteritems().


I still have this error : for name, layer in layerMap.items: TypeError: 'builtin_function_or_method' object is not iterable

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
GuenLM
  • 45
  • 3

1 Answers1

3

You should change two lines. The first one is about PyQGIS 3, the other is about Python 3.

  1. Change QgsMapLayerRegistry.instance().mapLayers() into QgsProject.instance().mapLayers(). Please check this answer out for more information.

  2. Change layerMap.iteritems() into layerMap.items(). Please check What is the difference between dict.items() and dict.iteritems()? and answers for additional information.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389