[vtkusers] VTK and PyQt
Jacky Ko
jackkykokoko at gmail.com
Thu Nov 19 00:59:13 EST 2015
This a reply for the thread VTK and PyQt
http://public.kitware.com/pipermail/vtkusers/2015-November/092811.html
Unlike C++ , PyQt has a special way for VTK embedding
Please follow the instruction to create a custom defined Qt widget:
1. Create a widget object in Qt Designer
2. In the top right Object Inspector panel, right click and
select "Promote to..."
3. a. Base class name: QWidget, b. Promoted class name: vtkWidget,
c. Header file: vtkWidget, click "add"
In this step, the promoted class name and header file need not to
be the same, but for convention I usually set them to be in same
name.
4. Save the UI design in the folder where your project is in, I
use main.ui for example
5. In the python project, create the vtkWidget.py header file with
following code:
import vtk
from PyQt4 import QtGui
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt4.QtGui import QLabel
class vtkWidget(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.vbl = QtGui.QVBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self)
self.vbl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.renWin = self.vtkWidget.GetRenderWindow()
self.vtkWidget.show()
self.iren.Initialize()
self.setLayout(self.vbl)
6. In the python project, create a main.py file with following
code:
from PyQt4 import QtCore, QtGui, uic
import vtk
import sys
form_class = uic.loadUiType("main.ui")[0]
class Ui_MainWindow(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
# setting renderer of 3D widget, object name of the vtkWidget in
.ui file is called widget3D
self.ren3D = vtk.vtkRenderer()
self.widget3D.vtkWidget.GetRenderWindow().AddRenderer(self.ren3D)
# render window interactor property
self.iren =
self.widget3D.vtkWidget.GetRenderWindow().GetInteractor()
self.iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
def Exit(self):
sys.exit(app.exec_())
app = QtGui.QApplication(sys.argv)
Ui_MainWindow = Ui_MainWindow(None)
Ui_MainWindow.show()
app.exec_()
7. Follow vtk pipeline to create an vtkActor class , you can
throw the actor into the vtkWidget by self.ren3D.AddActor() function.
Execute main.py and see if the widget worked fine. With the uic
load function, the UI can dynamically design with Qt Designer
instead of pre-compilation. Remember to save .ui file everytime after
modification .
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20151119/b4a883fe/attachment.html>
More information about the vtkusers
mailing list