[vtkusers] Embedding Python QVTKRenderWindowInteractor into Qt-Enviroment
Rocco Gasteiger
post at rocco-gasteiger.de
Fri May 22 09:44:07 EDT 2009
Hi Eric,
Thank you very much for your help! I post-process my compiled ui-file in
that way you described and it works now! Only some minor changes were
necessary: "from QVTKWidget import QVTKWidget" instead of 'from QVTK' and
'from vtk.qt4.QVTK' to 'from vtk.qt4.QVTKRenderWindowInteractor import
QVTKRenderWindowInteractor'
Best regards and have a nice weekend, Rocco
-----Original Message-----
From: Eric E. Monson [mailto:emonson at cs.duke.edu]
Sent: Wednesday, May 20, 2009 6:15 PM
To: Rocco Gasteiger
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Embedding Python QVTKRenderWindowInteractor into
Qt-Enviroment
Hey Rocco,
I don't know if this will help, but when I was trying to get PyQt to
work initially, I came up with a kind of hacky solution that works for
me. I lay out everything in Designer, just as I do when using C++,
including placing a QVTKWidget in the GUI. Then, after I save the .ui
file, instead of just running pyuic4 on that .ui file, I run a little
python script which does that plus makes a couple substitutions in the
resulting .py file. By doing this I don't have to add the
QVTKRenderWindowInteractor separately in my main python program.
=========== build.py
#!/usr/bin/env python
import os
command = "pyuic4 -o temp_simpleview.py qsimpleview.ui"
print 'Generating ui_simpleview.py from qsimpleview.ui'
os.system(command)
# Need to do some substitution in generated file to make the UI work
in python
# when the QVTKWidget was created to be used in C++
f = open('temp_simpleview.py', 'r')
s = f.read()
f.close()
s = s.replace('QVTKWidget','QVTKRenderWindowInteractor')
s = s.replace('from QVTK', 'from vtk.qt4.QVTK')
w = open('ui_simpleview.py', 'w')
w.write(s)
w.close()
os.remove('temp_simpleview.py')
===========
Then the constructor for my base class starts like this:
===========
from PyQt4 import QtCore, QtGui
from ui_simpleview import Ui_MainWindow
import vtk
class SimpleView(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ren = vtk.vtkRenderer()
self.ui.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
...
===========
Hope this helps,
-Eric
------------------------------------------------------
Eric E Monson
Duke Visualization Technology Group
On May 20, 2009, at 4:20 AM, Rocco Gasteiger wrote:
> Dear members,
>
> I want to render some vtk-data into a Qt-Enviroment via Python
> scripting. To
> do this, I use Qt-Designer, PyQt 4.4.4 and python binding for the vtk
> classes. In my old C++ implementation I use a QVTKWidget embedded
> into a
> QGroupBox for 2D/3D rendering and it works fine. For some reasons, I
> want to
> switch to do the same via python scripting. But here I have the
> following
> problem:
>
> A QVTKWidget does not exist in PyQt. So I found in a prior posting,
> that I
> have to use a QVTKRenderWindowInteractor class instead. In my python
> script
> I initialized this class with a QFrame-object, which I have defined
> and
> placed in the Qt-Designer before. It compiles correct and the render
> content
> is displayed into the QFrame-Object BUT with the wrong size. In my old
> C++-Implementation the size of the render window fits into the
> QVTKWidget
> according its size. Changing the size of render window by hand has
> no effect
> of the displayed render window. Can anybody give me a hint what I'm
> doing
> wrong or where my mistake is? For better understanding what I have
> done, I
> listed some code snipped from my python script below. Additionally I
> attached a picture of the running Qt-application.
>
> For any help I would be very appreciate.
> Best Regards, Rocco Gasteiger
>
> // This is the initializing part of my QVTKRenderWindowInteractor. The
> object "axialView" is a QFrame object
> // which I have defined in Qt-Designer
> def __init__(self, parent = None):
> """
> Constructor
> """
> QMainWindow.__init__(self, parent)
> self.setupUi(self)
> self.connect(self.actionOpenDICOM,
> QtCore.SIGNAL('triggered()'),
> self.loadFiles)
> self.axialViewerWidget =
> QVTKRenderWindowInteractor(self.axialView)
>
>
> self.axialViewerWidget.Initialize()
> self.axialViewerWidget.Start()
> self.initDatasets();
> self.initSliceViews();
>
> // Here I setup the the image viewer and its interactor.
> def initSliceViews(self):
> print 'Init Slice Views'
> axialViewer = vtk.vtkImageViewer2()
> self.axialViewer.SetInput(self.dataset)
>
> self.interactorForAxialView = vtk.vtkInteractorStyleImage()
>
> self
> .axialViewer
> .SetupInteractor(self.axialViewerWidget.GetRenderWindow().Ge
> tInteractor())
> self.interactorForAxialView =
> self.axialViewer.GetInteractorStyle()
>
> self
> .axialViewer.SetRenderWindow(self.axialViewerWidget.GetRenderWindow())
>
> self.axialViewerWidget.show()
> self.axialViewer.Render()
>
>
>
> <QtResult.png>_______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
More information about the vtkusers
mailing list