[Qgis-user] How to catch a QgsMapCanvas::emitExtentsChanged-signal?

Ludwig Kniprath ludwig at kni-online.de
Fri Mar 15 03:44:06 PDT 2024


Hi Bernhard, what about


iface.mapCanvas().extentsChanged.connect(lambda: print("the extent has 
changed!!!"))


Or something more usefull like


def extent_has_changed_slot():
     print(iface.mapCanvas().extent())

iface.mapCanvas().extentsChanged.connect(extent_has_changed_slot)


Btw, the *signal* is 'extentsChanged', 'emitExtentsChanged' is a 
*function*, which will trigger the emit of the signal.


So:

a) 'xxx' ist the signal-object, above 'iface.mapCanvas().extentsChanged'

b) extentsChanged is a signal without parameters, signals with 
parameters can be used in slot-functions with appropriate signatures.

Below a sample for a signal with parameters, see 
https://api.qgis.org/api/classQgsMapCanvas.html#a6c6f74644c9b273917a29e77b6400e89

def map_tool_was_set_slot(new_tool,old_tool):
     print(new_tool,old_tool)

iface.mapCanvas().mapToolSet.connect(map_tool_was_set_slot)


Hth

Ludwig


Am 14.03.24 um 15:48 schrieb Bernhard Seckinger via QGIS-User:
> Hi,
>
> I'm trying to write a plugin, that gets into action, whenever the user moves
> or zooms the main map. I found the QgsMapCanvas::emitExtentsChanged() signal.
> This is probably exactly, what I need. But I don't know, how to "catch" this
> signal. I guess, this is somehow done with pyqtSignals, but I don't understand
> how...
>
> I found a tutorial (1) which explains how to use pyqtSignal with self defined
> signals. I think I got this. But now I need to catch a signal which is defined
> in the library. How to connect to this signal?
>
>  From what I understand, my code should be something like:
>
> def __init__():
>      # with some pyqtSignal xxx:
>      xxx.connect(my_fun)
>
> def my_fun(some parameters?):
>      # code to execute when extents changed
>
> So two question remain:
>
> a) What is xxx above?
> b) Does "my_fun" need some parameters?
>
> Any help is appreciated!
>
> Thanks, Bernhard
>
> (1) https://gis-ops.com/qgis-3-plugin-tutorial-pyqt-signal-slot-explained/
>
> --
> Bernhard Seckinger <bernhard.seckinger at bueffee.de>
> _______________________________________________
> QGIS-User mailing list
> QGIS-User at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


More information about the QGIS-User mailing list