使用vtkTimerCallback与QVTKRenderWindowInteractor无法正常工作

时间:2018-04-12 00:35:31

标签: python callback pyqt vtk

我遇到了vtk(8.1),pyqt5(5.10.1)的问题。如果我使用vtkCallBackTimer与"原始" vtk.vtkRenderWindowInteractor()一切正常。但是如果我使用 vtk.qt.QVTKRenderWindowInteractor.QVTKRenderWindowInteractor(),则会连续执行回调函数而不会暂停。这是一个例子:

import sys
from PyQt5 import QtWidgets
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from mainwindow import Ui_MainWindow

class vtkTimerCallback():
    def __init__(self):
        self.timer_count = 0

    def execute(self, obj, event):
        print(self.timer_count)
        self.actor.SetPosition(self.timer_count, self.timer_count, 0)
        iren = obj
        iren.GetRenderWindow().Render()
        self.timer_count += 1

class ExampleApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super (ExampleApp, self).__init__(parent)
        self.setupUi(self)
        sphereSource = vtk.vtkSphereSource()
        sphereSource.SetCenter(0.0, 0.0, 0.0)
        sphereSource.SetRadius(5)
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(sphereSource.GetOutputPort())
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        renderer = vtk.vtkRenderer()
        renderWindow = vtk.vtkRenderWindow()
        renderWindow.AddRenderer(renderer)
        renderWindowInteractor = vtk.vtkRenderWindowInteractor # <-- WOKRING
        renderWindowInteractor = QVTKRenderWindowInteractor(self.frame) # <-- NOT WORKING (self.frame is a QFrame in the mainwindow.ui)
        renderWindowInteractor.SetRenderWindow(renderWindow)
        renderer.AddActor(actor)
        renderer.SetBackground(1, 1, 1)  # Background color white
        renderWindow.Render()
        renderWindowInteractor.Initialize()
        cb = vtkTimerCallback()
        cb.actor = actor
        renderWindowInteractor.AddObserver('TimerEvent', cb.execute)
        #****************
        renderWindowInteractor.CreateRepeatingTimer(1000) #Pause between new function call in millisecs,
        # with the QVTKRenderWindowInteractor the pause is ignored ?????
        #****************
        renderWindowInteractor.Start()

def main():
    app = QtWidgets.QApplication(sys.argv)
    trudeUI = ExampleApp()
    trudeUI.show()
    app.exec_()

if __name__ =='__main__':
    main()

我希望任何人都可以提供帮助。我需要RepeatingTimer来制作动画。如果某人有一个替代方案可以,但不是QtCore.QTimer。应该是一个vtk-thing ... 我现在尝试了4个小时,欢迎任何帮助...

1 个答案:

答案 0 :(得分:1)

也有同样的问题。在我看来,这似乎是QVTKRenderWindowInteractor类中的一个错误。 创建重复计时器时,不会考虑duration参数(在您的示例中为1000),而是使用默认参数(10)。

因此,要解决此问题,我创建了一个子类MyQVTKRenderWindowInteractor并重写了我需要的方法:

class MyQVTKRenderWindowInteractor(QVTKRenderWindowInteractor):
   def __init__(self, *arg):
       super(MyQVTKRenderWindowInteractor, self).__init__(*arg)
       self._TimerDuration = 10 # default value

   def CreateTimer(self, obj, event):
       self._Timer.start(self._TimerDuration) # self._Timer.start(10) in orginal

   def CreateRepeatingTimer(self, duration):
       self._TimerDuration = duration
       super(MyQVTKRenderWindowInteractor, self).GetRenderWindow().GetInteractor().CreateRepeatingTimer(duration)
       self._TimeDuration = 10

现在renderWindowInteractor = MyQVTKRenderWindowInteractor(self.frame)应该可以工作了。

跳跃的答案还没来得及 ++