Get Qt sender in Python slot

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 function does not work, because it returns None in all cases apparently.


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!

1 Like

This question has come up in the PythonQt discussion forum. There was no usable answer there (they were satisfied with using lambda), but you could ask again either there or on github.

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).

1 Like

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.

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 :slight_smile: )



Still worked great last time I checked a few months ago :+1: