Sep 1, 2009

Embedding vtk into a pyqt GUI

Now that vtk is installed with python bindings, all you need to do is to let your GUI find these bindings, then import them into your GUI. Let's jump right into the code. I have skipped some bits that are general for vtk, and have mainly included bits that are directly relevant to the QVTKRenderWindowInteractor.

import sys
// Now append the path of the python wrapping, which comes in the source code.
// Alter the path according to your own path.
sys.path.append("C:\Qt\vtk-5.4.2\Wrapping\Python")
//Import the vtk module
import vtk

class StartMain(QtGui.QMainWindow):
# no parent, i.e. is top level widget
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_main()
self.ui.setupUi(self)
self.ui.meshDisplayWidget = QVTKRenderWindowInteractor(self.ui.meshDisplay) self.ui.meshDisplayWidget.Initialize()

//Set actors and mappers, then instead of creating a renderwindowinteractor,
// use the self.ui.meshDisplayWidget to display the mesh. Also define picker, and
// set data source (for code about displaying a mesh from coordinates, as
// an unstructured grid.
ren = vtk.vtkRenderer()
ren.AddActor(aMeshActor)
ren.AddActor2D(textActor)
ren.AddActor(dotActor)

ren.ResetCamera()
cam1 = ren.GetActiveCamera()
self.ui.meshDisplayWidget.GetRenderWindow().AddRenderer(ren)
self.ui.meshDisplayWidget.SetPicker(picker_point)
self.ui.meshDisplayWidget.show()