0

我的问题是,如果选中该复选框,我想继续旋转场景,并在取消选中后立即停止旋转。但是,“继续旋转”意味着无限循环......

所以进入循环后,程序有点冻结,不再对我的“取消选中”信号做出反应。有没有办法中断这个循环?以下是相关代码的骨架。谢谢!

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)
4

1 回答 1

1

您不能将其作为循环来实现。这被定义为中断程序的交互,因为它阻止了 Qt 应用程序的“主循环”运行。

将您的绘图代码放入事件处理程序(如重绘事件),并使用计时器以固定间隔(例如 10/s)生成事件。

于 2009-11-19T07:40:49.023 回答