带有 Qt5 的 VTK - 与窗口交互时计时器停止运行 [英] VTK with Qt5 - Timer stops running when window is interacted with
问题描述
我一直在尝试用 VTK 做动画,所以我一直在使用 TimerEvent.当我试图转移到 Qt 绑定时,它坏了.问题是,一旦我与视图交互(比如滚动缩放或单击旋转),计时器就会停止.这是一个简单的最小示例:
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_()
这个脚本应该一遍又一遍地显示tick"和tock"这两个词,但只要你在窗口内点击就停止.
This script should display the words "tick" and "tock" over and over again, but stop as soon as you click inside the window.
一个奇怪的行为是按T"切换到轨迹球交互方式似乎有一些效果.如果我按 T 并然后在窗口内单击,则计时器只会在我单击时停止运行:当我松开时它会再次启动.如果我然后按 J 返回操纵杆模式",问题又回来了:点击永远停止计时器.
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.
Python 3.6, VTK 8, Qt 5.
推荐答案
该问题在 Linux 16.04、VTK8.1.1 和 Qt5.5.1 中可重现.
The problem is reproducible in Linux 16.04, VTK8.1.1 and Qt5.5.1.
当您使用 Qt 时,解决问题的方法是使用 QTimer()
.如果您想使用时间,这是一个解决方案.
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.
这是更改 QTimer()
的 TimerEvent
的最小示例:
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_()
这篇关于带有 Qt5 的 VTK - 与窗口交互时计时器停止运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!