Add an observer to the SliceNode's scrollbar

Hello!

I would like to add an observer to the scrollbar events of a given view. I cannot find the right event to add the callback function.

viewerNode = slicer.util.getNode("vtkMRMLSliceDisplayNode1") # Or should that be the vtkMRMLSliceNode class?
observerID = viewerNode.AddObserver(<WHICH_CLASS__WHICH_METHOD>, onScrollbarChanged)

The callback function:

def onScrollbarChanged(observer,eventID):
    print(<SLICE_INDEX>)

Thank you!

So, this will give you the qt object the represents the slider for a particular view (the red slice in this case)

slider = slicer.app.layoutManager().sliceWidget("Red").sliceController().sliceOffsetSlider()

you can then do something like:
slicer.app.layoutManager().sliceWidget("Red").sliceController().sliceOffsetSlider().valueIsChanging.connect(lambda x : print(x))

Operating system: Fedora Linux 39
Slicer Version: 5.6.1

Hello,
The method mentioned by Sam_Horvath valueIsChanging was not working for. I have added an Observer on the slice Logic and it works fine.

sliceLogic = slicer.app.layoutManager().sliceWidget(“Red”).sliceLogic()
sliceLogic.AddObserver(vtk.vtkCommand.ModifiedEvent, lambda caller, event: print(“test”))

However, I have noticed that this function will be triggered thrice so you should add a criteria to your function.

Cyprien