VTK with Qt5 - Timer stops running when window is interacted with

偶尔善良 提交于 2019-12-13 20:19:55

问题


I've been trying to do animation with VTK, so I've been using TimerEvent. When I tried to move over to the Qt binding, it broke. The problem is that as soon as I interact with the view (say scrolling to zoom, or clicking to rotate) the timer stops. Here's a simple minimal example:

import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5 import Qt

message = "tick"
def onTimerEvent(object, event):
    global message
    print(message)
    if message == "tick":
        message = "tock"
    else:
        message = "tick"

app = Qt.QApplication([])
mainWindow = Qt.QMainWindow()
renderer = vtk.vtkRenderer()
vtkWidget = QVTKRenderWindowInteractor(mainWindow)
vtkWidget.GetRenderWindow().AddRenderer(renderer)
mainWindow.setCentralWidget(vtkWidget)

vtkWidget.GetRenderWindow().GetInteractor().Initialize()
timerId = vtkWidget.CreateRepeatingTimer(100)
vtkWidget.AddObserver("TimerEvent", onTimerEvent)

mainWindow.show()
app.exec_()

This script should display the words "tick" and "tock" over and over again, but stop as soon as you click inside the window.

One odd behavior is that pressing "T" to switch to trackball interaction style seems to have some effect. If I press T and then click inside the window, the timer only stops running while I'm clicking: when I let go it starts up again. If I then press J to go back to "joystick mode", the problem returns: clicking stops the timer forever.

Python 3.6, VTK 8, Qt 5.


回答1:


The problem is reproducible in Linux 16.04, VTK8.1.1 and Qt5.5.1.

As you are using Qt, a workaround for your problem is to use QTimer(). It is a solution if you want to work with a timing.

This is your minimal example changing the TimerEvent for QTimer():

import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5 import Qt
from PyQt5.QtCore import QTimer

message = "tick"
def onTimerEvent():
    global message
    print(message)
    if message == "tick":
        message = "tock"
    else:
        message = "tick"

app = Qt.QApplication([])
mainWindow = Qt.QMainWindow()
renderer = vtk.vtkRenderer()
vtkWidget = QVTKRenderWindowInteractor(mainWindow)
vtkWidget.GetRenderWindow().AddRenderer(renderer)
mainWindow.setCentralWidget(vtkWidget)

vtkWidget.GetRenderWindow().GetInteractor().Initialize()
#timerId = vtkWidget.CreateRepeatingTimer(100)
#vtkWidget.AddObserver("TimerEvent", onTimerEvent)
timer = QTimer()
timer.timeout.connect(onTimerEvent)
timer.start(100)
mainWindow.show()
app.exec_()


来源:https://stackoverflow.com/questions/51285797/vtk-with-qt5-timer-stops-running-when-window-is-interacted-with

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!