# How to use signals and slots in Slicer 3d?

**URL:** https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013
**Category:** Support
**Created:** [October 13, 2020, 2:28pm UTC](https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013 "2020-10-13T14:28:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Chintha\_Siva\_Prasad](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chintha_siva_prasad/32/7766_2.png) [@Chintha\_Siva\_Prasad](https://discourse.slicer.org/u/Chintha_Siva_Prasad)
#### Post date: [October 13, 2020, 2:28pm UTC](https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013/1 "2020-10-13T14:28:14Z")

</div>

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.  
 ![Screenshot from 2020-10-13 19-57-03](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/a/ea5135400ea87fde32dacc513d5090c6ec905fdc.png)  
How to use signals and slots in slicer pythonqt?

---

<div class="post-metadata">

### Author: ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)
#### Post date: [October 13, 2020, 2:54pm UTC](https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013/2 "2020-10-13T14:54:08Z")

</div>

A previous post of mine mentioned some about basics of using Qt signals and slots with python.

> [@Dynamically Updating UI](https://discourse.slicer.org/t/dynamically-updating-ui/13801/6):
>
> Doing some like a=qt.QPushButton() is already the python interface that you can use in Slicer to interact with Qt which is C++ based. Slicer uses [PythonQt](https://mevislab.github.io/pythonqt/) and not [PyQt](https://riverbankcomputing.com/software/pyqt/intro). They are similar in syntax, but with different licensing. How are you currently connecting the button press to updating the label? I generally connect actions using the newer style of a=qt.QPushButton() a.clicked.connect(slotName)

**Essentially the syntax is something like {object}.{signal\_name}.connect({slot\_name}).**

So something that is based on QProgressBar will have [these signals](https://doc.qt.io/qt-5/qprogressbar.html#signals) such as valueChanged. Therefore

```python
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"

```

---

<div class="post-metadata">

### Author: ![Chintha\_Siva\_Prasad](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chintha_siva_prasad/32/7766_2.png) [@Chintha\_Siva\_Prasad](https://discourse.slicer.org/u/Chintha_Siva_Prasad)
#### Post date: [October 14, 2020, 2:20pm UTC](https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013/3 "2020-10-14T14:20:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chintha\_Siva\_Prasad](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chintha_siva_prasad/32/7766_2.png) [@Chintha\_Siva\_Prasad](https://discourse.slicer.org/u/Chintha_Siva_Prasad)
#### Post date: [October 14, 2020, 2:23pm UTC](https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013/4 "2020-10-14T14:23:37Z")

</div>

![Screenshot from 2020-10-13 19-57-03](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/a/ea5135400ea87fde32dacc513d5090c6ec905fdc.png)

When I am executing the above code slicer is exiting abnormally. How can I create a new signal in slicer that works fine?

---

<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: [October 15, 2020, 4:43am UTC](https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013/5 "2020-10-15T04:43:04Z")

</div>

This example works for me:

```python
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](https://discourse.slicer.org/t/custom-signal-slots-with-pythonqt/3278/6) and [PythonQt / Discussion / Help: Segmentation fault after emiting a signal](https://sourceforge.net/p/pythonqt/discussion/631393/thread/bbc98b8f/))

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:

> [@Custom Signal/Slots with PythonQt](https://discourse.slicer.org/t/custom-signal-slots-with-pythonqt/3278/8):
>
> 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.

What is your use case? What would you like to achieve with the custom signals/slots?

---

<div class="post-metadata">

### Author: ![Chintha\_Siva\_Prasad](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chintha_siva_prasad/32/7766_2.png) [@Chintha\_Siva\_Prasad](https://discourse.slicer.org/u/Chintha_Siva_Prasad)
#### Post date: [October 18, 2020, 3:18pm UTC](https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013/6 "2020-10-18T15:18:00Z")

</div>

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…
