# 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:** 1
**Showing post:** 5

<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?

---

_[View the full topic](https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013)._
