在尝试了几件事后,我得到了解决方案,阅读以下内容 合并请求 。
我使用QGLWidget作为QVTKRenderWindowInteractor的基类,而不是QWidget。此更改是因为据报道有时QWidget会导致渲染问题。
为此,我在导入QVTKRenderWindowInteractor之前输入以下代码:
import vtk.qt vtk.qt.QVTKRWIBase = "QGLWidget" from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
为了使用QGLWidget,我必须安装以下软件包:
sudo apt-get install python3-pyqt5.qtopengl
正如我在评论中所写,我无法在MacOS 10.11.6上重现VTK 8.1的问题。如果您运行的是旧版本,请考虑升级。
下面是另一个例子 QVTKRenderWindowInteractor 小部件嵌入在 QMainWindow 。问题是否仍然存在?
QVTKRenderWindowInteractor
QMainWindow
如果是,请比较输出 print(self.ren) 我附在下面。我会报告这个 vtk问题跟踪器 。
print(self.ren)
#!/usr/bin/env python import sys import vtk from PyQt5 import QtWidgets from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor from vtk.util.colors import tomato class MainWindow(QtWidgets.QMainWindow): def __init__(self, parent = None): super(MainWindow, self,).__init__(parent) self.frame = QtWidgets.QFrame() self.vl = QtWidgets.QVBoxLayout() self.vtkWidget = QVTKRenderWindowInteractor(self.frame) self.vl.addWidget(self.vtkWidget) self.ren = vtk.vtkRenderer() self.ren.SetBackground(0.5,0.5,0.5) self.vtkWidget.GetRenderWindow().AddRenderer(self.ren) self.iren = self.vtkWidget.GetRenderWindow().GetInteractor() source = vtk.vtkConeSource() source.SetResolution(8) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(source.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetColor(tomato) actor.RotateX(30.0) actor.RotateY(-45.0) self.ren.AddActor(actor) self.ren.SetBackground(0.1, 0.2, 0.4) self.ren.ResetCamera() self.ren.GetActiveCamera().Zoom(1.5) self.frame.setLayout(self.vl) self.setCentralWidget(self.frame) # Print the renderer. print(self.ren) self.show() self.iren.Initialize() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) window = MainWindow() sys.exit(app.exec_())
控制台输出:
vtkOpenGLRenderer (0x7ff4c3d8a5f0) Debug: Off Modified Time: 1159 Reference Count: 2 Registered Events: (none) Aspect: (1, 1) PixelAspect: (1, 1) Background: (0.1, 0.2, 0.4) Background2: (0.2, 0.2, 0.2) BackgroundAlpha: 0 GradientBackground: Off Viewport: (0, 0, 1, 1) Displaypoint: (0, 0, 0) Viewpoint: (0, 0, 0) Worldpoint: (0, 0, 0, 0) Pick Position X1 Y1: -1 -1 Pick Position X2 Y2: -1 -1 IsPicking boolean: 0 Props: Debug: Off Modified Time: 923 Reference Count: 1 Registered Events: (none) Number Of Items: 1 PickResultProps: nullptr Near Clipping Plane Tolerance: 0.001 ClippingRangeExpansion: 0.5 Ambient: (1, 1, 1) Backing Store: Off Display Point: (0, 0, 0) Lights: Debug: Off Modified Time: 806 Reference Count: 1 Registered Events: (none) Number Of Items: 0 Light Follow Camera: On View Point: (0, 0, 0) Two Sided Lighting: On Automatic Light Creation: On Layer = 0 PreserveDepthBuffer: Off PreserveColorBuffer: Off Interactive = On Allocated Render Time: 10000 Last Time To Render (Seconds): -1 TimeFactor: 1 Erase: On Draw: On UseDepthPeeling: Off OcclusionRation: 0 MaximumNumberOfPeels: 4 LastRenderingUsedDepthPeeling: Off Delegate:null Selector: 0x0 TexturedBackground: Off BackgroundTexture:null Pass:null PickedId0 NumPicked0 PickedZ 0