[vtkusers] vtkChartXY, QVTKRenderWindowInteractor, PyQt4

Randy Heiland heiland at indiana.edu
Fri Jul 22 17:47:11 EDT 2011


Hi gang,

I'm having problems getting a vtkChartXY displayed in PyQt4.  I saw a thread related to this (vtkContextView and QWidget), but didn't see a solution for Python-land.  I'll include 2 example scripts - the 1st works, the 2nd does not.

thanks for any help, Randy

#
#  -------  gui-cone.py ------------

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

class test(Qt.QMainWindow):
    def __init__(self, parent=None):
         Qt.QMainWindow.__init__(self, parent)
         self.setWindowTitle(self.tr("PyQt4 vtkConeSource"))
         self.workspace = Qt.QWorkspace()
         self.setCentralWidget(self.workspace)
         self.frame = QtGui.QFrame(self.workspace)
         self.hbox = QtGui.QHBoxLayout()

         self.widget = QVTKRenderWindowInteractor(self.frame)
         self.widget.Initialize()
         self.widget.Start()

         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 = vtk.vtkRenderer()
         self.ren.AddActor(self.coneActor)
         self.renWin = self.widget.GetRenderWindow()
         self.renWin.AddRenderer(self.ren)

         self.hbox.addWidget(self.widget)
         self.frame.setLayout(self.hbox)
         self.workspace.addWindow(self.frame)

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

#--------------------------------------------------------------
#
#  -------  gui-chart.py ------------
import sys
from vtk import *
from vtk.qt4.QVTKRenderWindowInteractor  import QVTKRenderWindowInteractor 
from PyQt4 import Qt 
from PyQt4 import QtGui 

class test(Qt.QMainWindow):
    def __init__(self, parent=None):
        Qt.QMainWindow.__init__(self, parent)
        self.setWindowTitle(self.tr("PyQt4 vtkChartXY"))
        self.workspace = Qt.QWorkspace()
        self.setCentralWidget(self.workspace)
        self.frame = QtGui.QFrame(self.workspace)
        self.hbox = QtGui.QHBoxLayout()

        self.widget = QVTKRenderWindowInteractor(self.frame)
        self.widget.Initialize()
        self.widget.Start()

        self.table = vtkTable()
        self.arrX = vtkFloatArray()
        self.arrX.SetName("X Axis")
        self.table.AddColumn(self.arrX)

        self.arrY = vtkFloatArray()
        self.arrY.SetName("Y value")
        self.table.AddColumn(self.arrY)

        numPoints = 20
        inc = 7.5 / (numPoints-1)
        self.table.SetNumberOfRows(numPoints)
        for i in range(numPoints):
            self.table.SetValue(i, 0, i * inc)
            self.table.SetValue(i, 1, i * inc)

        self.view = vtkContextView()
        self.view.GetRenderer().SetBackground(1.0, 1.0, 1.0)

        self.chart = vtkChartXY()
        self.view.GetScene().AddItem(self.chart)
        self.line = self.chart.AddPlot(vtkChart.LINE)
        self.line.SetInput(self.table, 0, 1)

        self.ren = self.view.GetRenderer()
        self.renWin = self.view.GetRenderWindow()
        self.renWin.AddRenderer(self.ren)

        self.hbox.addWidget(self.widget)
        self.frame.setLayout(self.hbox)
        self.workspace.addWindow(self.frame)

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




More information about the vtkusers mailing list