1

With QGIS 2.18, I used to capture the composer closure with PyQGIS, the class QgsComposerView and its composerViewHide signal:

self.obj_compo.composerView.composerViewHide.connect(self.raiseModule)

Now, there is the QGIS 3 answer:

AttributeError: 'composerClass' object has no attribute 'composerView'

I guess this is because "composer related methods have been removed from the public API and Python bindings. These classes have been replaced with the new layouts engine, based on QgsLayout, QgsLayoutItem, and the other related classes."

But I can't find a signal in QgsLayout class or a parent to capture the composer closure. If someone has any clues...

2

The QgisInterface class has the following signals which you should be able to make work for you:

layoutDesignerClosed

layoutDesignerOpened

layoutDesignerWillBeClosed

These signals will be emitted when any print layout is opened/closed etc. but the latter two also emit the QgsLayoutDesignerInterface object which you can catch in a slot function. So, if you only want to target a specific layout you could do something like:

def layoutClosed(designer):
    if designer.window().windowTitle() == 'Some layout name':
        #Do something here

iface.layoutDesignerWillBeClosed.connect(layoutClosed)

The above is just an example which I tested in the Python console. It seems your code is inside a plugin so you will need to modify accordingly e.g. put the signal/slot connection inside the initGui(self) method, and make iface and slot function instance attribute/ method etc.

To be honest, while this should work, it doesn't seem particularly elegant but I can't readily see a better solution. If someone knows a better way- I will be glad to learn it.

2
  • Thanks a lot. It worked great. Do you know why using layoutDesignerwillBeClosed signal, and not layoutDesignerClosed ?
    – V Damoy
    Nov 19 '20 at 14:34
  • @V Damoy, if you look at the docs for these signals you will see that layoutDesignerWillBeClosed is "Emitted before a layout designer is going to be closed and deleted" & also allows you to catch the designer object so you can do something with it before it's deleted. Whereas layoutDesignerClosed is emitted after a layout is closed & doesn't emit the designer object. It's just a signal that a layout has been closed. So basically, if you want to be able to do something with a layout object before it's closed you need to use layoutDesignerWillBeClosed.
    – Ben W
    Nov 20 '20 at 0:15

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.