如何取消选中复选框以停止 pyqt 中的无限绘图?
我的问题是,如果选中该复选框,我想继续旋转场景,并在取消选中后立即停止该旋转。然而,“继续旋转”意味着无限循环......
因此,进入循环后,程序会被冻结,不再对我的“取消选中”信号做出反应。有没有办法中断这个循环?以下是相关代码的框架。 谢谢!
class Draw(QGLWidget):
def __init__(...):
...
self.rotate=0
self.auto=False
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glRotatef(self.rotate,0.0,0.0,1.0)
draw stuff...
glFlush()
def autoRotate(self,auto): # auto is an integer and used here as true/false
self.auto=auto
while self.auto:
self.rotate+=0.5
if self.rotate>360:
self.rotate-=360
self.updateGL()
if auto==False:
break
class SpiralWidgetDemo(QtGui.QMainWindow):
def __init__(self):
...
auto=QtGui.QCheckBox("Auto")
self.connect(auto,QtCore.SIGNAL("stateChanged(int)"),widget.autoRotate)
My problem is I want to keep rotating the scene if the checkbox is checked, and stop this rotation immediately once it is unchecked. However, "keep rotating" means an infinite loop...
So after entering the loop, the program gets kind of freezed and no longer react to my "uncheck" signal. Is there a way to interrupt this loop? The following is skeleton of related code.
Thanks!
class Draw(QGLWidget):
def __init__(...):
...
self.rotate=0
self.auto=False
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glRotatef(self.rotate,0.0,0.0,1.0)
draw stuff...
glFlush()
def autoRotate(self,auto): # auto is an integer and used here as true/false
self.auto=auto
while self.auto:
self.rotate+=0.5
if self.rotate>360:
self.rotate-=360
self.updateGL()
if auto==False:
break
class SpiralWidgetDemo(QtGui.QMainWindow):
def __init__(self):
...
auto=QtGui.QCheckBox("Auto")
self.connect(auto,QtCore.SIGNAL("stateChanged(int)"),widget.autoRotate)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能将其实现为循环。其定义是为了破坏程序的交互,因为它阻止 Qt 应用程序的“主循环”运行。
将绘图代码放入事件处理程序(如重绘事件)中,并使用计时器定期生成事件(例如 10/s)。
You must not implement this as a loop. This is defined to break the interaction of the program, as it prevents the "main loop" of the Qt application from running.
Put your drawing code into an event handler (like redraw event), and use a timer to generate events at regular intervals (e.g. 10/s).