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

0

1 Answer 1

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.

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.