<div dir="ltr"><div>This a  reply for  the  thread  VTK and PyQt</div><div><br></div><a href="http://public.kitware.com/pipermail/vtkusers/2015-November/092811.html">http://public.kitware.com/pipermail/vtkusers/2015-November/092811.html</a><br><div><br></div><div>Unlike  C++ ,  PyQt  has a  special  way  for  VTK  embedding</div><div><br></div><div>Please  follow  the  instruction  to  create a  custom  defined  Qt widget:</div><div>1.  Create a  widget  object in  Qt  Designer</div><div>2.  In  the  top  right  Object  Inspector  panel,  right click   and  select  "Promote to..."</div><div>3.  a. Base class  name: QWidget,  b.  Promoted class name:  vtkWidget,  c.  Header  file:  vtkWidget,  click  "add"</div><div>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.</div><div>4.  Save the  UI design  in the  folder  where  your  project is  in,  I use  main.ui  for  example</div><div>5.  <span style="font-family:arial,sans-serif">In the  python  project,  create  the  vtkWidget.py header file  with following  code:</span></div><div><span style="font-family:arial,sans-serif"><br></span></div><div><div style="">import vtk</div><div style="">from PyQt4 import QtGui</div><div style="">from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor</div><div style="">from PyQt4.QtGui import QLabel</div><div style=""><br></div><div style="">class vtkWidget(QtGui.QWidget):</div><div style=""><br></div><div style="">    def __init__(self, parent = None):</div><div style="">        QtGui.QWidget.__init__(self, parent)</div><div style="">        self.vbl = QtGui.QVBoxLayout()</div><div style=""><br></div><div style="">        self.vtkWidget = QVTKRenderWindowInteractor(self)</div><div style="">        self.vbl.addWidget(self.vtkWidget)</div><div style="">        </div><div style="">        self.ren = vtk.vtkRenderer()</div><div style="">        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)</div><div style="">        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()</div><div style="">        self.renWin = self.vtkWidget.GetRenderWindow()</div><div style="">        </div><div style="">        self.vtkWidget.show()</div><div style="">        self.iren.Initialize()</div><div style="">        self.setLayout(self.vbl)</div></div><div><span style="font-family:arial,sans-serif"><br></span></div><div>6.  In the  python  project,  create  a  main.py  file  with following  code:</div><div><br></div><div>from PyQt4 import QtCore, QtGui, uic<br></div><div>import vtk<br></div><div>import sys</div><div><br></div><div>form_class = uic.loadUiType("main.ui")[0]<br></div><div><div>class Ui_MainWindow(QtGui.QMainWindow, form_class):</div><div>    def __init__(self, parent=None):</div><div>        QtGui.QMainWindow.__init__(self, parent)</div><div>        self.setupUi(self)</div></div><div><br></div><div><div>      # setting renderer of 3D widget, object  name  of  the  vtkWidget  in  .ui  file is  called  widget3D</div><div>        self.ren3D = vtk.vtkRenderer()</div><div>        self.widget3D.vtkWidget.GetRenderWindow().AddRenderer(self.ren3D)</div><div>        </div><div>        # render window interactor property</div><div>        self.iren = self.widget3D.vtkWidget.GetRenderWindow().GetInteractor()</div><div>        self.iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())</div></div><div><br></div><div><div>    def Exit(self):</div><div>        sys.exit(app.exec_())</div><div><br></div><div>app = QtGui.QApplication(sys.argv)</div><div>Ui_MainWindow = Ui_MainWindow(None)</div><div>Ui_MainWindow.show()</div><div>app.exec_()</div></div><div><br></div><div>7.  Follow  vtk  pipeline  to  create  an  vtkActor  class ,  you  can throw  the  actor  into the  vtkWidget  by  self.ren3D.AddActor()  function.</div><div><br></div><div>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 .</div></div>