[vtkusers] embedded VTK window in PyQt application

John Vines (CISD/HPCD) jvines at arl.army.mil
Wed May 13 09:04:05 EDT 2009


All,
    I am trying to embed a VTK renderwindow in a PyQt application and am 
not sure where I am going wrong.    I am using VTK 5.2.1 built with QVTK 
support, Qt 4.3.3 and PyQt 4.3.3.  Any pointers would be greatly 
appreciated.

example python code:

#!/usr/bin/env python

import sys
import vtk
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt4 import Qt

class test(Qt.QMainWindow):
    """Test class"""
    def __init__(self, parent=None):
        Qt.QMainWindow.__init__(self, parent)
        self.setWindowTitle(self.tr("PyQt4 VTK test"))
       
        self.workspace = Qt.QWorkspace()
        self.setCentralWidget(self.workspace)

        # create the widget
        self.widget = QVTKRenderWindowInteractor()
        self.widget.Initialize()
        self.widget.Start()

        # if you dont want the 'q' key to exit comment this.
        self.widget.AddObserver("ExitEvent", lambda o, e, a=app: a.quit())
   
        self.ren = vtk.vtkRenderer()
        self.widget.GetRenderWindow().AddRenderer(self.ren)
   
        self.cone = vtk.vtkConeSource()
        self.cone.SetResolution(8)
       
        self.coneMapper = vtk.vtkPolyDataMapper()
        self.coneMapper.SetInput(self.cone.GetOutput())
       
        self.coneActor = vtk.vtkActor()
        self.coneActor.SetMapper(self.coneMapper)
   
        self.ren.AddActor(self.coneActor)
        self.workspace.addWindow(self.widget)

if __name__ == "__main__":
    app = Qt.QApplication(sys.argv)
    mainwindow = test()
    mainwindow.show()
    sys.exit(app.exec_())

Thanks,
John



More information about the vtkusers mailing list