2

I am required to display a QMessageBox.warning() as a reminder to update a specific field when "Toggle editing" is clicked. This is required for a project in QGIS. Currently, I just can display the QmessageBox to notice which layer is active

from PyQt5.QtWidgets import QMessageBox

from qgis.core import QgsProject

layer=qgis.utils.iface.mapCanvas().currentLayer()

QMessageBox.warning(iface.mainWindow(), "Capa Activa", 'La capa activa es ' +layer.name())
3
  • 1
    I guess your options here are edit the QGIS source code (and maybe make this behaviour switchable on a user option) or add a new action button via a plugin that pops up the dialog before enabling editing, and hiding or disabling the standard one.
    – Spacedman
    Oct 5, 2022 at 11:28
  • 1
    @Spacedman No need to change QGIS source code, there is signal and slots in PyQGIS to connect a custom Python function to an existing event. I'm waiting for the question to open again.
    – etrimaille
    Oct 5, 2022 at 16:06
  • @etrimaille indeed, I wasn't sure if clicking action buttons generated signals that could be caught in Python code... Q is reopened, happy hacking...
    – Spacedman
    Oct 6, 2022 at 8:58

1 Answer 1

2

You need to "connect" your function to the signal beforeEditingStarted belonging to the QgsVectorLayer class. https://api.qgis.org/api/classQgsVectorLayer.html#a817d41d8320a54b2d51fbcb54e7fe2dd

from qgis.PyQt.QtWidgets import QMessageBox
from qgis.core import QgsProject

def start_editing():
    layer = iface.activeLayer()
    QMessageBox.warning(
        iface.mainWindow(),
        "Capa Activa",
        f'The layer {layer.name()} is now in editing mode.'
    )

layer = iface.activeLayer()
layer.beforeEditingStarted.connect(start_editing)

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.