If you need to perform any actions just before the window closes, connect the aboutToCloseWidget event of the Window part to an action in your part.
If you need to perform any actions just before the window opens, connect the aboutToOpenWidget event of the Window part to an action in your part.
If the menu attribute is connected to a Menu part, that menu part is displayed unless you change the menu attribute as a result of this event. You can change the menu attribute to include your own menu or leave it unchanged to use the original setting.