[vtkusers] Question about QVTKRenderWindowInteractor inside a Qt class

Tony Willis Tony.Willis at nrc-cnrc.gc.ca
Fri Mar 25 03:34:37 EST 2005


Hi

I'm a Newbie to VTK who would like to do some visualization with VTK
inside a larger application built with python and the Qt widget set.
My VTK window interactor has to be buried inside a larger Qt
class so that I can click on various buttons etc to control the
way the application behaves. For my own amusement (and because
it closely resembles what I want to do) I'm trying to recode
the VTK/Examples/GUI/Python/ImagePlaneWidget.py in Qt. I encountered
a few problems there so went back to basics ...

I took the QVTKRenderWidgetConeExample demo function
in QVTKRenderWindowInteractor.py and turned it
into a python class (see below). Things work OK
when I use explicit setGeometry and SetSize calls in the class
(see below) but these two calls were not needed in the
original QVTKRenderWidgetConeExample function.

However, if I leave them out of my class construction, then
the QVTKRenderWindowInteractor does not behave properly.
In particular, holding the right mouse
button down causes the cone to shrink down in size regardless of
whether the mouse is in the upper or lower half of the window at
that moment. (I'm using VTK 4.4, but the QVTKRenderWindowInteractor.py
in the current CVS appears only to have been upgraded for some
stereo vision stuff so I don't think it will some my problem.

I'd be grateful if some VTK / Qt guru can tell me why these
setGeometry and SetSize calls are needed here, and how they
can be avoided, if possible.

Thanks

Tony

#-----------------------------------------------------------------------
import qt
import vtk
from vtk.qt.QVTKRenderWindowInteractor import *

# adaption of the function 'QVTKRenderWidgetConeExample'
# in QVTKRenderWindowInteractor.py to be a Qt-style class

class QVTKRenderWidgetConeExample(qt.QWidget):
  """A simple example that uses the QVTKRenderWindowInteractor
  class.  """

  def __init__( self, *args ):
    qt.QWidget.__init__(self, *args)

    # create the QVTKRenderWindowInteractor widget
    widget = QVTKRenderWindowInteractor(self)
    # if you dont want the 'q' key to exit comment this.
    widget.AddObserver("ExitEvent", lambda o, e, a=app: a.quit())

#Thanks to Patrick Brockmann for his examples
#using setGeometry and SetSize
#http://dods.ipsl.jussieu.fr/brocksce/vtkqt

# why is the following line needed?
    widget.setGeometry(qt.QRect(20,20,600,400))
    ren = vtk.vtkRenderer()
    widget.GetRenderWindow().AddRenderer(ren)
# why is the following line needed?
    widget.GetRenderWindow().SetSize(600,400)

# standard cone stuff
    cone = vtk.vtkConeSource()
    cone.SetResolution(16)
    coneMapper = vtk.vtkPolyDataMapper()
    coneMapper.SetInput(cone.GetOutput())
    coneActor = vtk.vtkActor()
    coneActor.SetMapper(coneMapper)
    ren.AddActor(coneActor)

if __name__ == "__main__":
    # std QT stuff ...
    app = qt.QApplication(['QVTKRenderWindowInteractor'])
    # create the widget
    widget = QVTKRenderWidgetConeExample()
    widget.show()
    # start event processing
    app.exec_loop()
#-----------------------------------------------------------------------
___________
Tony Willis
National Research Council   Tony.Willis at nrc-cnrc.gc.ca
Box 248                     (250)493-2277
Penticton, BC  V2A 6J9      fax: 493-7767
Government of Canada        Gouvernement du Canada




More information about the vtkusers mailing list