# Custom Events for VTK

**URL:** <https://discourse.slicer.org/t/custom-events-for-vtk/1286>\
**Category:** Support\
**Created:** [October 25, 2017, 5:41pm UTC](https://discourse.slicer.org/t/custom-events-for-vtk/1286 "2017-10-25T17:41:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomasyi17](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@thomasyi17](https://discourse.slicer.org/u/thomasyi17)\
**Post date:** [October 25, 2017, 5:41pm UTC](https://discourse.slicer.org/t/custom-events-for-vtk/1286/1 "2017-10-25T17:41:41Z")

</div>

Hello,

I was wondering if there is a convenient way to define custom vtk events (preferably at a high level) and thereafter attach a data payload to them?

As an analogous example, the qt slider widget has a valueChanged signal that has an attached payload containing the new value. In the context of vtk, one could then do something along the lines of (perhaps as a static variable):

var = vtk.vtkCommand.UserEvent + 100

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [October 25, 2017, 6:35pm UTC](https://discourse.slicer.org/t/custom-events-for-vtk/1286/2 "2017-10-25T18:35:29Z")

</div>

Examples of the stages of creating and using such a custom event (through SegmentModified):

- [Definition](https://github.com/Slicer/Slicer/blob/master/Libs/vtkSegmentationCore/vtkSegmentation.h#L102)
- [Invocation with “payload” data](https://github.com/Slicer/Slicer/blob/master/Libs/vtkSegmentationCore/vtkSegmentation.cxx#L577)
- [Making connection](https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/qSlicerSegmentationsModule.cxx#L265)
- [Getting the “payload” data](https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/SubjectHierarchyPlugins/qSlicerSubjectHierarchySegmentationsPlugin.cxx#L606)

Handling the event is possible in Python as well (see example for [connecting](https://github.com/SlicerRt/FilmDosimetryAnalysis/blob/master/FilmDosimetryAnalysis/FilmDosimetryAnalysis.py#L123) and [using](https://github.com/SlicerRt/FilmDosimetryAnalysis/blob/master/FilmDosimetryAnalysis/FilmDosimetryAnalysis.py#L934)), but I’m not sure if you can define such an event from Python.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [October 25, 2017, 8:28pm UTC](https://discourse.slicer.org/t/custom-events-for-vtk/1286/3 "2017-10-25T20:28:33Z")

</div>

For some types of data can add a decorator to your callback that interprets the call data in python.

[https://github.com/Kitware/VTK/blob/master/Wrapping/Python/vtk/util/misc.py#L6-L27](https://github.com/Kitware/VTK/blob/master/Wrapping/Python/vtk/util/misc.py#L6-L27)

For most data types in Slicer, a vtkObject is actually returned as a wrapped version of whatever concrete subclass was passed in the payload, so it actually covers all slicer events that come from nodes or the scene or other classes.

> <https://github.com/Slicer/Slicer/blob/ee5c8703642cf8f42e9594bc780cb0801623b39d/Modules/Scripted/DICOMLib/DICOMWidgets.py#L828-L832>

You can also call InvokeEvent from python with a calldata (payload) argument.

In practice it’s often the case in Slicer that you don’t need calldata because knowing the caller is enough to get the information you want because what would otherwise be the payload data has already been set in the corresponding instance variable of the caller.
