I think the main issue here is, that there is no option to set the auto-refresh rate of print-layout-maps (at least none that I know of). So you can use data defined override for your extents as below, but the print-layout-canvas will only refresh if you manually click on the refresh button, minimize and remaximize the print layout window, or something like that. Which makes it a little bit pointless... But in my eyes this would be a nice feature though.
However, you could create a custom function like the following:
@qgsfunction(args='auto', group='Custom')
def my_canvas(feature, parent):
e = QgsGeometry.fromWkt(qgis.utils.iface.mapCanvas().extent().asWktPolygon())
return e
It returns the geometry of your current main-window-map-canvas. You can then use x_max(my_canvas())
, x_min(my_canvas())
, y_min(my_canvas())
, y_max(my_canvas())
as expressions for data defined override for your print-layout-map properties:
PS: I am not an expert in PyQGIS, so I dont know why I had to use .asWktPolygon()
as step inbetween. It just failed returning QgsGeometry()
directly when using .asPolygon()
.