# Detect when slice layer changes

**URL:** <https://discourse.slicer.org/t/detect-when-slice-layer-changes/12775>\
**Category:** Development\
**Created:** [July 29, 2020, 7:47pm UTC](https://discourse.slicer.org/t/detect-when-slice-layer-changes/12775 "2020-07-29T19:47:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakew](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jakew](https://discourse.slicer.org/u/Jakew)\
**Post date:** [July 29, 2020, 7:47pm UTC](https://discourse.slicer.org/t/detect-when-slice-layer-changes/12775/1 "2020-07-29T19:47:23Z")

</div>

I’m looking for a way to detect when the scroll bar at the top of a view moves, or when the slice view changes. This can be done by detecting when the scroll wheel moves or when arrow keys are used, but I’d like to be able to detect the event itself. Is there a signal like that that can initiate a function when detected?

---

<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:** [July 29, 2020, 8:03pm UTC](https://discourse.slicer.org/t/detect-when-slice-layer-changes/12775/2 "2020-07-29T20:03:58Z")

</div>

The suggested way is add an observer to the vtkMRMLSlliceNode corresponding to the view, but this won’t tell you how (mouse vs keyboard) the slice offset was modified, just that it did move. What’s your use case?

---

<div class="post-metadata">

**Author:** ![Jakew](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jakew](https://discourse.slicer.org/u/Jakew)\
**Post date:** [July 29, 2020, 9:07pm UTC](https://discourse.slicer.org/t/detect-when-slice-layer-changes/12775/3 "2020-07-29T21:07:12Z")

</div>

Yes my intended use is for just to see it was modified. Thanks for pointing me in the right direction!

---

<div class="post-metadata">

**Author:** ![Jakew](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jakew](https://discourse.slicer.org/u/Jakew)\
**Post date:** [July 30, 2020, 11:49pm UTC](https://discourse.slicer.org/t/detect-when-slice-layer-changes/12775/4 "2020-07-30T23:49:04Z")

</div>

Just wanted to confirm, is the observer method doable in C++ or just Python? I’m looking into C++ observer methods and it doesn’t seem to be able to be used for MRMLSliceNodes or MRMLScenes.

---

<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:** [July 31, 2020, 2:58am UTC](https://discourse.slicer.org/t/detect-when-slice-layer-changes/12775/5 "2020-07-31T02:58:17Z")

</div>

All Slicer features are available in both C++ and Python. In Slicer source code there are many examples for adding observers: search for `AddObserver` if you are interested in adding observer from VTK classes, and search for `qvtkReconnect` if you want to add observer from Qt classes.
