# Safe way to handle QT events

**URL:** <https://discourse.slicer.org/t/safe-way-to-handle-qt-events/41686>\
**Category:** Development\
**Created:** [February 13, 2025, 6:32pm UTC](https://discourse.slicer.org/t/safe-way-to-handle-qt-events/41686 "2025-02-13T18:32:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DouSam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dousam/32/79250_2.png) [@DouSam](https://discourse.slicer.org/u/DouSam)\
**Post date:** [February 13, 2025, 6:32pm UTC](https://discourse.slicer.org/t/safe-way-to-handle-qt-events/41686/1 "2025-02-13T18:32:23Z")

</div>

We are working on the [TutorialMaker](https://github.com/SlicerLatinAmerica/SlicerTutorialMaker/) extension. One of the processes is to execute the tutorial, you can see one [here](https://github.com/SlicerLatinAmerica/TestSlicerTutorials/blob/main/Tutorials/FourMinuteTutorial/FourMinuteTutorial.py).  
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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/f/7f174c58469a8bd70cc8daadbaa670f172fca76e.png)  
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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/8/1857c45421deaa7d61017d418490edb35a16ef93.png)

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/9/99b795ff7b95df5490fb6528e0f9d2155fcdcb63.png)  
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 13, 2025, 8:21pm UTC](https://discourse.slicer.org/t/safe-way-to-handle-qt-events/41686/2 "2025-02-13T20:21:26Z")

</div>

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.
