2

I created a very simple plugin with the plugin builder for GIS 3.4. Wihtout any GUI-Widget - by clicking the plugin button I just want to save the edits from several layers at one time and start the edit mode again. That works fine. But in the end I want to refresh the map (what I can do by clicking seperatly the refresh button, but I would like to have this function in my plugin). I tried it like this (but the last row is not working):

def run(self):
    """Run method that performs all the real work"""
    layer_edit = [ 'neuer Baumpunkt', 'baeume_akt','kontrolle','schaeden_akt','massnahmen_akt']
    price = 6.0
    for layers in QgsProject.instance().mapLayers().values():
        if layers.name() in layer_edit:
            layers.commitChanges()
            layers.startEditing()
            layers.refresh()

But there is an AttributeError:'QgsVectorLayer' object has no attribute 'refresh' The same is with refreshAllLayers(). I'm only started working with python, so I'm a very beginner.

I tried what Germán Carrillo suggested like this:

def run(self):
    """Run method that performs all the real work"""
    layer_edit = [ 'neuer Baumpunkt', 'baeume_akt','kontrolle','schaeden_akt','massnahmen_akt']
    price = 6.0
    for layers in QgsProject.instance().mapLayers().values():
        if layers.name() in layer_edit:
            layers.commitChanges()
            layers.startEditing()
    self.iface.mapCanvas().refresh()

But it doesn't work.

The beginning of my skript looks like this:

from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction
from qgis.core import QgsProject
# Initialize Qt resources from file resources.py
from .resources import *
# Import the code for the dialog
from .bk_save_dialog import BK_speichernDialog
import os.path

class BK_speichern:
"""QGIS Plugin Implementation."""

   def __init__(self, iface):
       """Constructor.

       :param iface: An interface instance that will be passed to this class
           which provides the hook by which you can manipulate the QGIS
           application at run time.
       :type iface: QgsInterface
       """
       # Save reference to the QGIS interface
       self.iface = iface
       # initialize plugin directory
       self.plugin_dir = os.path.dirname(__file__)
       # initialize locale
       locale = QSettings().value('locale/userLocale')[0:2]
       locale_path = os.path.join(
           self.plugin_dir,
           'i18n',
           'BK_speichern_{}.qm'.format(locale))

       if os.path.exists(locale_path):
           self.translator = QTranslator()
           self.translator.load(locale_path)
           QCoreApplication.installTranslator(self.translator)

       # Declare instance attributes
       self.actions = []
       self.menu = self.tr(u'&Baumkontrolle speichern')

       # Check if plugin was started the first time in current QGIS session
       # Must be set in initGui() to survive plugin reloads
       self.first_start = None
  • Unfortunately it does not work. – Nane Jan 12 at 22:20
2

To refresh all layers, you can use the following command : iface.mapCanvas().refreshAllLayers()

In your case : self.iface.mapCanvas().refreshAllLayers()

The following script work (I write out of a function) :

from qgis.utils import iface

layer_edit = [ 'Enveloppe concave', 'Sortie SQL','Channel']
for layers in QgsProject.instance().mapLayers().values():
    if layers.name() in layer_edit:
        layers.commitChanges()
        layers.startEditing()

    iface.mapCanvas().refreshAllLayers()`
| improve this answer | |
  • I got the error NameError: name 'iface' is not defined. How/where can I define it? I will add the beginning of the skript in my question. I just startet with python... – Nane Jan 13 at 10:06
  • add this line at the beginning : from qgis.utils import iface – Vincent Bré Jan 13 at 10:08
  • 1
    it works like this! Thank you very much! – Nane Jan 13 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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