Safe way to handle QT events

We are working on the TutorialMaker extension. One of the processes is to execute the tutorial, you can see one here.
The idea is to recreate the steps like a user normally does and take a screenshot after this step, but we are having problems with the qt events. For example:
Simulate that click to open the Red Slice Controller


We have tried to use the click event on the pin button and the show event directly on the SlicerController and we have the same problem. Sometimes in the screenshot, that event was processed and appears on the screenshot and sometimes not.
image

image
The problem is, that we don’t have control if the event is processed or not, and that variable from OS to OS, on MAC, tends to be more broken, and the event is processed two or three screenshots later.

We have to use .update() on the widget after and before the event and the slicer.app.processEvents() before taking the screenshot, but that makes our extension slow.

My question here is, how we should replicate these events? Like this click to open the Slicer Controller on the screenshot. Is there a way to control that?
Something like:

clickEvent = await widget.click()

So we know when we call the Tutorial.nextScreenshot() this popout will appear on the screenshot.

Obs: We know that is possible to manipulate the model using the slicer API, and we do that, but it’s important to show the Slicer Controller so the user who is reading the Tutorial knows where he clicks to manipulate the red slice.

The slice view controller widget is displayed with a quick animation. You may need to wait a bit to get it fully displayed.

I would recommend to try to execute the tutorial script with a QTimer. This way, instead your Python script of taking over the the applicaiton and then trying to give some time to the application to process events; the application would run as normal. The actions you need to do would be performed one by one, in the timer callback.