vtk:如何删除所有观察者
vtk: how to remove all observers
我在我的项目中使用 VTK。现在,我想删除所有观察者并实现我自己的观察者。但是 RemoveAllObservers
不起作用。我的代码是:
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_()
即使我通过interactor.RemoveAllObservers()
移除所有观察者,图像仍然可以通过鼠标右键缩放in/out。
我怎样才能删除所有的观察者?
问题出在您的设置顺序上。
imageViewer.SetupInteractor()
重新创建一些默认观察者。所以你应该在之后删除/创建观察者。
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()
我在我的项目中使用 VTK。现在,我想删除所有观察者并实现我自己的观察者。但是 RemoveAllObservers
不起作用。我的代码是:
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_()
即使我通过interactor.RemoveAllObservers()
移除所有观察者,图像仍然可以通过鼠标右键缩放in/out。
我怎样才能删除所有的观察者?
问题出在您的设置顺序上。
imageViewer.SetupInteractor()
重新创建一些默认观察者。所以你应该在之后删除/创建观察者。
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()