the signal/slot system of Qt is a very helpful mechanism for implementing callbacks. I was wondering if I can create my own signals and slots with PythonQt but had no success so far.
In other projects where I use e.g. pySide I can subclass from “QObject” and then define my custom signals which then can be connected to my callbacks. When searching in PythonQt threads I found a similar code snippet here:
Unfortunately I am not able to get a PythonQt.QtCore.QObject object in the Python version of Slicer. Is this some wanted restriction?
If this is not possible, is there a similar mechansim available? As an example: I have an IGTL connection which receives data. Based on the data I want to update some UI elements. The connection class and the Widget class are two separate classes though. I am trying to keep the logic separated from the UI.
It would be good to know if PythonQt supports creating signals directly and if it does we can add it to Slicer. There are limits compared with PySide, but it gets better over time. Did you look at the PythonQt page and check with the folks there?
I did some more tests and still have some problems using the signal-slot concept via Qt. To beginn with I forgot to mention that I was using the stable release of Slicer. Therefore, there was no qt.Signal or qt.Slot object available, only a qt.SIGNAL which would not work.
After updating to a nightly release version of Slicer, the qt.Signal and qt.Slot classes were available. I was still not able though to connect a simple signal with a slot. Everytime I try to connect, the whole application just crashes, e.g.:
Other similar tries (e.g. without qt.Slot decorator) lead to a crash or a non-working connection. In the end I just tried to wrap the signal-slot connection into a class which derives from QWidget (since QObject is not available). This seems to solve the issue. The signal is connected and also correctly triggered.
It still though does not feel right to subclass from QWidget for every class that needs signal-slot feature. Therefore I might use an alternative solution, e.g. the PySignals module. Or are there any better proposals for me?
@jcfr I looked through the commits after r455, and didn’t see anything that was obviously related… I wonder if something from CTK is shadowing/removing QObject, because it is registered the same way as many other classes which are available under PythonQt.QtCore in the repl (e.g. QMimeData).
Hi all,
I’m interested in emitting a signal and, if some object is registered on this “channel”, in receiving it.
Any idea about which qt tool can I use?
I also read this and I got a crash too.
We have signal/slot mechanism in Qt and observer mechanism in VTK, and you can implement custom plugin or callback mechanisms. These are all available to be used from both C++ and Python.
Probably the most universal way (that is available from all Slicer classes) is to observe/invoke custom modified events on a MRML node.