python example 에서 vtk renderwindow 를 qt에 embeding 하여 실행 할 수 있는 예제를 제공하고 있다.
하지만, 모듈의 업데이트로 인하여 작동하지 않음.
기존 예제파일은 다음과 같이 변경해야 한다.
https://www.vtk.org/Wiki/VTK/Examples/Python/Widgets/EmbedPyQt
#!/usr/bin/env python
import sys
import vtk
from PyQt5 import QtCore, QtGui
from PyQt5 import Qt
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class MainWindow(Qt.QMainWindow):
def __init__(self, parent=None):
Qt.QMainWindow.__init__(self, parent)
self.frame = Qt.QFrame()
self.vl = Qt.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()
# 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)
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.show()
self.iren.Initialize()
self.iren.Start()
if __name__ == "__main__":
app = Qt.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
추가적으로 아래의 글에 따르면, interactor를 여러개 불러들이는 경우 Start()시에 이벤트를 등록하는것이 qt에 불필요하게 많이 등록된다. 따라서 전체적으로 필요한 작업을 모두 완료 한 뒤에 Start()를 실행할 것 또한 이는 app.exec 를 마지막에 실행해야 한다고 한다.
Important note: if your qt application becomes increasingly complex and you will be using multiple QVTKRenderWindowInteractor objects in it, do not call the interactor through the Start() method. Otherwise, as I mentioned before, you are creating another unnecessary event loop inside your qt application (app.exec() starts the qt loop). In this case, I think you should call app.exec() after you declared you necessary objects. More information can be found in these links:
- https://www.vtk.org/doc/nightly/html/classQVTKInteractor.html#details
- https://www.vtk.org/pipermail/vtkusers/2009-February/050560.html
- https://public.kitware.com/pipermail/vtkusers/2006-July/036328.html