0

I have the same problem as Making QGIS layer update from changed data source.

However, I cannot get the solution proposed by Matthias Kuhn in a comment on an answer to that question to work.

I am using QGIS 3.4, I have loaded a .geojson file as a vector layer and am trying to refresh it without removing and re-adding. The map refreshes when I move it but the attribute table is not re-loaded. I may not have understood how to use the reload function so here are my steps, I am using the Python Console.

       mc = iface.mapCanvas()
       layer = mc.currentLayer()
       layer.name()
       'radarOutGJTemp'  #--- Correct

       # Tried the following with errors attached
       layer.dataProvider().reload()
AttributeError: 'QgsVectorDataProvider' object has no attribute 'reload'  

       layer.dataProvider.reload('radarOutGJTemp')
AttributeError: 'builtin_function_or_method' object has no attribute 'reload'  

       layer.dataProvider.reload(radarOutGJTemp)
AttributeError: 'builtin_function_or_method' object has no attribute 'reload'

I can see the function reload appear in a window as I type. I did try to import reload just in case, that failed but again I could see it appear in the window as I typed.

import reload
ModuleNotFoundError: No module named 'reload'
5
  • 1
    The method reload() belongs to the QgsRasterDataProvider class. You are working with a QgsVectorDataProvider object. This class has the method forceReload(). Did you try that?
    – Ben W
    Jun 7, 2020 at 22:02
  • Your answer makes sense, I had not realised. I think I may be missing something basic, this is my first attempt at Python in Qgis although I am programming in Python.
    – Roo
    Jun 8, 2020 at 7:38
  • I get AttributeError: 'builtin_function_or_method' object has no attribute 'forceReload' for all variations of the line. I can select the function so it does exist. I had missed the first setup step from my question so will add it to the code above.
    – Roo
    Jun 8, 2020 at 7:46
  • 1
    That suggests that you didn't include the parentheses after dataProvider() needed to actually call the method, layer.dataProvider().forceReload() should work. However, I see that forceReload() is deprecated and will be removed in Qgis 4. So you could use the reloadData() method inherited by all subclasses of QgsDataProvider. E.g. layer.dataProvider().reloadData(). I haven't tested mind you- I'm just going off the docs.
    – Ben W
    Jun 8, 2020 at 8:05
  • That was it, I am sorry, parenthesis were missing. I will do a bit more testing and post an update.
    – Roo
    Jun 8, 2020 at 8:48

1 Answer 1

2

With help from @BenW, the following lines refresh my map when I run them in the QGIS Python console using QGIS v3.4.

  mc = iface.mapCanvas()
  layer = mc.currentLayer()
  layer.dataProvider().reloadData()
  layer.triggerRepaint()

The final step is explained in the following post.
Does mapCanvas().refresh() not work in QGIS 2.6?

Your Answer

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

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