vtkBoxWidget 不适用于 PyQt5 应用程序
vtkBoxWidget not working with PyQt5 application
我正在尝试使用 QVTKRenderWindowInteractor 在 PyQt5 中嵌入 vtk 渲染代码。当我尝试在其中使用 vtkBoxWidget 时,程序崩溃了。没有 vtkBoxWidget(包含在下面代码的两行内),它工作正常。你能帮我找到问题吗?我正在尝试在 vtkBoxWidget 的帮助下使用边界框手动裁剪渲染对象。
代码:
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QFrame, QVBoxLayout
import sys
import vtk
from PyQt5 import QtGui
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.frame = QFrame()
self.vl = QVBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.vl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
# Create source
source = vtk.vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(5.0)
# Create a mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
###################################################################################
boxWidget = vtk.vtkBoxWidget()
boxWidget.SetInteractor(self.iren)
boxWidget.SetPlaceFactor(1.0)
boxWidget.SetRotationEnabled(0)
planes = vtk.vtkPlanes()
def ClipVolumeRender(obj, event):
obj.GetPlanes(planes)
mapper.SetClippingPlanes(planes)
boxWidget.SetProp3D(actor)
boxWidget.PlaceWidget()
boxWidget.InsideOutOn()
boxWidget.AddObserver("InteractionEvent", ClipVolumeRender)
boxWidget.On()
###################################################################################
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.show()
self.iren.Initialize()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
问题是 boxWidget 是被 python 消除的局部变量,因此之后应用程序尝试访问该对象,但它不再分配内存,从而产生 分段错误。解决方案是通过将 boxWidget
更改为 self.boxWidget
.
来扩展范围
我正在尝试使用 QVTKRenderWindowInteractor 在 PyQt5 中嵌入 vtk 渲染代码。当我尝试在其中使用 vtkBoxWidget 时,程序崩溃了。没有 vtkBoxWidget(包含在下面代码的两行内),它工作正常。你能帮我找到问题吗?我正在尝试在 vtkBoxWidget 的帮助下使用边界框手动裁剪渲染对象。
代码:
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QFrame, QVBoxLayout
import sys
import vtk
from PyQt5 import QtGui
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.frame = QFrame()
self.vl = QVBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.vl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
# Create source
source = vtk.vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(5.0)
# Create a mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
###################################################################################
boxWidget = vtk.vtkBoxWidget()
boxWidget.SetInteractor(self.iren)
boxWidget.SetPlaceFactor(1.0)
boxWidget.SetRotationEnabled(0)
planes = vtk.vtkPlanes()
def ClipVolumeRender(obj, event):
obj.GetPlanes(planes)
mapper.SetClippingPlanes(planes)
boxWidget.SetProp3D(actor)
boxWidget.PlaceWidget()
boxWidget.InsideOutOn()
boxWidget.AddObserver("InteractionEvent", ClipVolumeRender)
boxWidget.On()
###################################################################################
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.show()
self.iren.Initialize()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
问题是 boxWidget 是被 python 消除的局部变量,因此之后应用程序尝试访问该对象,但它不再分配内存,从而产生 分段错误。解决方案是通过将 boxWidget
更改为 self.boxWidget
.