# Get Qt sender in Python slot

**URL:** <https://discourse.slicer.org/t/get-qt-sender-in-python-slot/18247>\
**Category:** Development\
**Tags:** python, qt\
**Created:** [June 21, 2021, 10:42am UTC](https://discourse.slicer.org/t/get-qt-sender-in-python-slot/18247 "2021-06-21T10:42:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [June 21, 2021, 10:42am UTC](https://discourse.slicer.org/t/get-qt-sender-in-python-slot/18247/1 "2021-06-21T10:42:46Z")

</div>

Hi all,

I have been debugging a strange issue, and maybe someone here can help.

When I load a scene in a Slicer custom app, an SH combobox `currentItemChanged` signal is emitted from an unknown place. There is nothing in the code that would set the selection programmatically (only the usual one in `updateGUIFromMRML` but with `blockSignals`), and I confirmed that it is not triggered by `setMRMLScene`. Unfortunately without knowing where this signal comes from, I cannot fix this issue in a good way.

My question is: how to get the sender in a Python slot? The [sender](https://doc.qt.io/qt-5/qobject.html#sender) function does not work, because it returns `None` in all cases apparently.

Thanks!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 21, 2021, 12:57pm UTC](https://discourse.slicer.org/t/get-qt-sender-in-python-slot/18247/2 "2021-06-21T12:57:31Z")

</div>

If you hook up a debugger you can set a breakpoint in the slot (or in a C++ method you call from the python code if needed) and the stacktrace should give you a hint, particularly if the signal comes from C++. Even if it comes from python you will see if it comes from a timer or other event. Good luck!

---

<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:** [June 21, 2021, 1:25pm UTC](https://discourse.slicer.org/t/get-qt-sender-in-python-slot/18247/3 "2021-06-21T13:25:49Z")

</div>

This question has come up in the [PythonQt discussion forum](https://sourceforge.net/p/pythonqt/discussion/631393/thread/a5cae166/?limit=25#db84). There was no usable answer there (they were satisfied with using lambda), but you could ask again either there or on [github](https://github.com/MeVisLab/pythonqt/issues).

I agree with @pieper that running it in a debugger seems to be a good option. I think Visual Studio can even do mixed debugging - step through Python and C++ stacks (but I don’t remember if I have ever used it or just read about it).

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [June 21, 2021, 3:43pm UTC](https://discourse.slicer.org/t/get-qt-sender-in-python-slot/18247/4 "2021-06-21T15:43:24Z")

</div>

Thank you for the answers! Luckily I think this specific issue can be debugged on Windows (the application is primarily for Linux and some in-house libraries are Linux only) and I’ll try the suggested approach on Windows.

---

<div class="post-metadata">

**Author:** ![finetjul](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/finetjul/32/146_2.png) [@finetjul](https://discourse.slicer.org/u/finetjul)\
**Post date:** [June 21, 2021, 4:01pm UTC](https://discourse.slicer.org/t/get-qt-sender-in-python-slot/18247/5 "2021-06-21T16:01:09Z")

</div>

`currentItemChanged` may get called when your model gets populated (when the first item is added) or unpopulated (when the current item gets removed). By observing all the signals emitted on the `model` itself that may give you a clue.

Alternatively, you should be able to set breakpoint with QtCreator on Linux (it was working back in the days 🙂 )

Hth,  
Julien.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 21, 2021, 4:06pm UTC](https://discourse.slicer.org/t/get-qt-sender-in-python-slot/18247/6 "2021-06-21T16:06:38Z")

</div>

> [@finetjul](#):
>
> Alternatively, you should be able to set breakpoint with QtCreator on Linux (it was working back in the days 🙂 )

Still worked great last time I checked a few months ago 👍
