[vtkusers] "Hello World" for QVTKWidget using PyQt and VTK in Python

Eric E. Monson emonson at cs.duke.edu
Wed Nov 3 09:52:03 EDT 2010


Hello Prathamesh,

Yes, it would help us diagnose your problems if you tell us which version of VTK you are using, on what platform, and whether you built it yourself (with VTK_WRAP_PYTHON_SIP: ON, which is required for the Python wrapping of QVTKWidget).

I've had some troubles with getting installed versions of VTK to work automatically with the SIP Python wrapped classes, so for now I just set my library path and pythonpath to directories in the build tree (this is with bash on OS X):

export VTK_DIR=/Users/emonson/Programming/VTK_git/VTK/build
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:${VTK_DIR}/bin
export PYTHONPATH=$PYTHONPATH:${VTK_DIR}/bin:${VTK_DIR}/Wrapping/Python

Once everything is built correctly and the paths are set, then import vtk.qvtk should work. I'll attach a simple example at the end in case you need a script example.

Also, just in case you didn't know, there is still the older QVTKRenderWindowInteractor which can be used to add a VTK render window to a PyQt app without building the SIP Python wrappers -- it's limited in a couple ways, but basically works fine for many applications.

Talk to you later,
-Eric

------------------------------------------------------
Eric E Monson
Duke Visualization Technology Group


# ==============================
from PyQt4 import QtCore, QtGui
import vtk
import sys

class Ui_MainWindow(object):
	def setupUi(self, MainWindow):
		MainWindow.setObjectName("MainWindow")
		MainWindow.resize(400, 400)
		self.centralWidget = QtGui.QWidget(MainWindow)
		self.gridlayout = QtGui.QGridLayout(self.centralWidget)
		self.vtkWidget = vtk.QVTKWidget(self.centralWidget)
		self.gridlayout.addWidget(self.vtkWidget, 0, 0, 1, 1)
		MainWindow.setCentralWidget(self.centralWidget)

class SimpleView(QtGui.QMainWindow):
    
	def __init__(self, parent = None):
	
		QtGui.QMainWindow.__init__(self, parent)
		self.ui = Ui_MainWindow() 
		self.ui.setupUi(self)
		
		widget = self.ui.vtkWidget
		
		ren = vtk.vtkRenderer()
		renwin = widget.GetRenderWindow()
		renwin.AddRenderer(ren)
		
		cone = vtk.vtkConeSource()
		mapper = vtk.vtkPolyDataMapper()
		mapper.SetInputConnection(cone.GetOutputPort())
		actor = vtk.vtkActor()
		actor.SetMapper(mapper)
		
		ren.AddViewProp(actor)
		ren.ResetCamera()
				
if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    window = SimpleView()
    window.show()
    sys.exit(app.exec_())


On Nov 3, 2010, at 6:18 AM, Jothy wrote:

> Which python bundle are you using and how are importing the QVTK?
> 
> One easiest option is to add the QVTKWidget into your working directory and import.
> 
> Jothy
> 
> On Wed, Nov 3, 2010 at 3:45 AM, Prathamesh Kulkarni <prathameshmkulkarni at gmail.com> wrote:
> Hello all,
> 
> I want to create a GUI using PyQt4 and display a vtkImage inside it. For that, I need to embed a QVTKWidget in my GUI. However, I get 'ImportError: No module named qvtk' when I do import vtk.qvtk as given in http://vtk.1045678.n5.nabble.com/about-vtk-python-bindings-and-Qt-td2841114i20.html . I am confused about how to get started with this and did not find examples demonstrating this. 
> 
> Please point me to the steps for writing this "hello world".
> 
> Thanks,
> Prathamesh
> 
> _______________________________________________
> 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
> 
> 
> _______________________________________________
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20101103/16458761/attachment.htm>


More information about the vtkusers mailing list