Issue
I am using VTK in my project. Now, I want to remove all observers and implement my-self observer. But the RemoveAllObservers
do not work. My code is:
from PyQt5.QtWidgets import *
import sys, os
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
def mouseMoveEvent(iren, event):
print('move')
class MyWin(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.setLayout(layout)
self.vtkWidget = QVTKRenderWindowInteractor()
layout.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
interactor = self.vtkWidget.GetRenderWindow().GetInteractor()
interactor.Initialize()
interactor.RemoveAllObservers()
interactor.AddObserver('MouseMoveEvent', mouseMoveEvent)
interactor.Start()
self.imageViewer = vtk.vtkImageViewer2()
self.imageViewer.SetupInteractor(interactor)
self.imageViewer.SetRenderWindow(self.vtkWidget.GetRenderWindow())
img = vtk.vtkDICOMImageReader()
img.SetFileName('C:\\Users\\MLoong\\Desktop\\dicom_data\\Chang Cheng\\TOF\\IM_0174')
img.Update()
self.imageViewer.SetInputData(img.GetOutput())
self.imageViewer.Render()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWin()
window.show()
app.exec_()
Even I remove all observers by interactor.RemoveAllObservers()
, the image can still be zoom in/out by right mouse.
How can I remove all the observers?
Solution
The problem is the order of your setup.
imageViewer.SetupInteractor()
recreate some default observers. So you should remove / create observer after.
interactor = self.vtkWidget.GetRenderWindow().GetInteractor()
self.imageViewer = vtk.vtkImageViewer2()
self.imageViewer.SetupInteractor(interactor)
self.imageViewer.SetRenderWindow(self.vtkWidget.GetRenderWindow())
# img = vtk.vtkDICOMImageReader()
# img.SetFileName('C:\\Users\\MLoong\\Desktop\\dicom_data\\Chang Cheng\\TOF\\IM_0174')
# using a source for testing
img = vtk.vtkRTAnalyticSource()
img.Update()
self.imageViewer.SetInputData(img.GetOutput())
self.imageViewer.Render()
interactor.RemoveAllObservers()
interactor.AddObserver('MouseMoveEvent', mouseMoveEvent)
interactor.Initialize()
interactor.Start()
Answered By - Nico Vuaille
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.