I am familiar with pyqt. I want to use signals and slots mechanism in my slicer python script. For some reason when I am connecting the signal to the slot, slicer is exiting abnormally.
How to use signals and slots in slicer pythonqt?
A previous post of mine mentioned some about basics of using Qt signals and slots with python.
Essentially the syntax is something like {object}.{signal_name}.connect({slot_name}).
So something that is based on QProgressBar will have these signals such as valueChanged. Therefore
def printMyNewValue(value):
print("The progress bar value is now: {}".format(value))
import qt
progress_bar = qt.QProgressBar()
progress_bar.setMaximum(10)
progress_bar.valueChanged.connect(printMyNewValue)
progress_bar.setValue(4) # will then print "The progress bar value is now: 4"
I want to create a new signal and then connect it to a slot. I don’t want to use the pre-defined signals of widgets.
When I am executing the above code slicer is exiting abnormally. How can I create a new signal in slicer that works fine?
This example works for me:
class C(qt.QObject):
received = qt.Signal(object)
def __init__(self):
super(C, self).__init__(None)
def process(self, msg):
self.received.emit(msg)
class D(qt.QObject):
def __init__(self, sender):
super(D, self).__init__(None)
sender.received.connect(self.process)
def process(self, msg):
print("Processed this: "+repr(msg))
c = C()
d = D(c)
m = {'h': {'t': 's'}, 'c': {'e': 'i'}}
c.process(m)
(taken from these discussions: Custom Signal/Slots with PythonQt - #6 by ihnorton and PythonQt / Discussion / Help: Segmentation fault after emiting a signal)
However, normally there is no need to create Qt-based Python classes and define new signals and slots, as you can communicate via MRML nodes and VTK event observations:
What is your use case? What would you like to achieve with the custom signals/slots?
I want to change the progress of a progressbar from another thread.
i want to create a signal(int) and connect it to progressBar.setValue(int).
But when I am connecting it is exiting…