如何在pyqt中取消选中复选框以停止无限绘图?

0 投票
1 回答
686 浏览
提问于 2025-04-15 16:09

我的问题是,我想在复选框被选中时让场景持续旋转,而一旦取消选中,就立刻停止这个旋转。不过,“持续旋转”意味着要进入一个无限循环……

所以,一旦进入这个循环,程序就会有点卡住,无法再对我取消选中的操作做出反应。有没有办法打断这个循环呢?下面是相关代码的框架。

谢谢!

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)

1 个回答

1

你不能把这个做成一个循环。这会影响程序的正常运行,因为它会阻止Qt应用程序的“主循环”继续工作。

把你的绘图代码放到一个事件处理器里(比如重绘事件),然后用一个定时器来定期生成事件(比如每秒10次)。

撰写回答