我需要得到一个vtkChartParallelCoordinates和其他使用与PyQt应用程序并行运行的vtkContextView的图。问题在于它们都使用用户鼠标交互的无限循环,并且我一次只能运行其中的一个。当我通过view.GetInteractor()启动vtk交互器时,Start()在关闭vtk窗口之前不会显示PyQt应用程序。 我想,我对如何做两种选择:VTK交互器和PyQt并行运行
- 照顾VTK对象的用户交互的PyQt的的循环手动
- 渲染PyQt的应用
关于第二个选项:我不能使用QVTKRenderWindowInteractor,它似乎不能使用vtkContextView图形。我发现Kitware的一个文档: http://www.na-mic.org/Wiki/images/1/18/NA-MIC-VTK-Charts-2011.pdf 在第22页他们使用QVTKWidget但我的编译vtk没有它。
我试图做一些关于选项1没有成功,没有相关的例子可用。
在我的代码下面,当我注释掉“view.GetInteractor().Start()”时,PyQt窗口显示并且是交互式的。
我在linux上使用Python 2.7.11版本,vtk版本7.0.0。
我会感谢任何帮助!
from PyQt4 import QtCore, QtGui
import vtk
import math
class Ui_widgetParallel(object):
def setupUi(self, widgetParallel):
widgetParallel.setObjectName("widgetParallel")
widgetParallel.resize(802, 651)
#button
self.button = QtGui.QPushButton(widgetParallel)
self.button.setGeometry(QtCore.QRect(180, 100, 75, 23))
self.button.setText("Click on me")
QtCore.QMetaObject.connectSlotsByName(widgetParallel)
self.button.clicked.connect(self.testClick)
def testClick(self):
print('I was clicked on')
def selectionCallback(caller, event):
#executes when new data is selected by the user
#prints row numbers of all selected data rows
annSel = annotationLink.GetCurrentSelection()
if annSel.GetNumberOfNodes() > 0:
idxArr = annSel.GetNode(0).GetSelectionList()
if idxArr.GetNumberOfTuples() > 0:
for ii in range(idxArr.GetNumberOfTuples()):
print(idxArr.GetValue(ii))
if __name__ == "__main__":
import sys
############################
# CREATE A DATA TABLE
############################
arrX = vtk.vtkFloatArray()
arrX.SetName("XAxis")
arrC = vtk.vtkFloatArray()
arrC.SetName("Cosine")
arrS = vtk.vtkFloatArray()
arrS.SetName("Sine")
arrS2 = vtk.vtkFloatArray()
arrS2.SetName("Tan")
numPoints = 20
inc = 0.2/(numPoints-1)
for i in range(numPoints):
arrX.InsertNextValue(i * inc)
arrC.InsertNextValue(math.cos(i * inc) + 0.0)
arrS.InsertNextValue(math.sin(i * inc) + 0.0)
arrS2.InsertNextValue(math.tan(i * inc) + 0.5)
table = vtk.vtkTable()
table.AddColumn(arrX)
table.AddColumn(arrC)
table.AddColumn(arrS)
table.AddColumn(arrS2)
############################
# STARTS THE QtGui application
############################
app = QtGui.QApplication(sys.argv)
widgetParallel = QtGui.QWidget()
ui = Ui_widgetParallel()
ui.setupUi(widgetParallel)
widgetParallel.show()
############################
# PARALLEL COORDINATES VIEW AND ANNOTATION
############################
#render contextView and parallel coordinates view
view = vtk.vtkContextView()
view.GetRenderer().SetBackground(1.0, 1.0, 1.0)
view.GetRenderWindow().SetSize(600,300)
chart = vtk.vtkChartParallelCoordinates()
view.GetScene().AddItem(chart)
# Create a annotation link to access selection in parallel coordinates view
annotationLink = vtk.vtkAnnotationLink()
annotationLink.GetCurrentSelection().GetNode(0).SetFieldType(1) # Point
annotationLink.GetCurrentSelection().GetNode(0).SetContentType(4) # 1 = GlobalIds, 2 = PedigreeIds, 4 = Indices
chart.SetAnnotationLink(annotationLink)
annotationLink.AddObserver("AnnotationChangedEvent", selectionCallback)
#link input data and refresh attributes view
chart.GetPlot(0).SetInputData(table)
chart.GetPlot(0).SetScalarVisibility(1)
chart.GetPlot(0).SetScalarVisibility(1)
chart.GetPlot(0).SetWidth(5)
chart.GetPlot(0).SetOpacity(0)
#render view
view.ResetCamera()
view.GetRenderWindow().SetMultiSamples(0)
view.Render()
view.GetInteractor().Start()
############################
# EXITS THE APPLICATION WHEN GUI LOOP IS CLOSED
############################
sys.exit(app.exec_())
这正是我所期待的,非常感谢! – Kubik