pyside6中Qobject内部定时器中遇到的问题及解决方案

from PySide6.QtCore import QObject


class Motors(QObject):
    def __init__(self,start:bool=None,stop:bool=None,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.start = start
        self.stop = stop
        self.time_stop_id = None
        self.time_start_id = None


    def run(self):
        if self.start and not self.stop:
            if  self.time_stop_id is not None :
                self.killTimer(self.time_stop_id)
            self.time_start_id = self.startTimer(100)

        if self.stop and not self.start:
            if self.time_start_id is not None:
                self.killTimer(self.time_start_id)
            self.time_stop_id = self.startTimer(100)


    def timerEvent(self, event):

        if event.timerId()==self.time_start_id:
            print("开始定时器中事件")
            print(event.timerId())

        if event.timerId() == self.time_stop_id:
            print("停止定时器中事件")
            print(event.timerId())




from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *



class Ui(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(600,600)
        self.btn = QPushButton("btn1",self)
        self.btn2 = QPushButton("btn2",self)
        self.btn.move(100,50)
        self.btn2.move(100,150)

        self.btn.clicked.connect(self.btn_click)
        self.btn2.clicked.connect(self.btn_click2)

        self.motor = Motors()

    def btn_click(self):

        self.motor.start = 1
        self.motor.stop = 0
        self.motor.run()
    def btn_click2(self):
        self.motor.start = 0
        self.motor.stop = 1
        self.motor.run()


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    win = Ui()
    win.show()

    sys.exit(app.exec())

上面的定时器两个都会执行,停止不了。

`Motors`类中的`run`方法逻辑存在一些问题,导致定时器两个都执行的原因是因为在`run`方法中,即使关闭了一个`timeId`,但是在下一次执行`run`方法时又会重新开启一个新的`timeId`,导致两个定时器都在运行。

为了解决这个问题,可以在`run`方法中增加判断,只有当`timeId`不存在时才开启新的定时器。同时,在关闭定时器时,将`timeId`置为`None`,以确保只有一个定时器在运行。

以下是修改后的`Motors`类的代码片段:
from PySide6.QtCore import QObject
class Motors(QObject):
    def __init__(self,start:bool=None,stop:bool=None,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.start = start
        self.stop = stop
        self.time_stop_id = None
        self.time_start_id = None

    def run(self):
        if self.start and not self.stop:
            if self.time_stop_id is not None:
                self.killTimer(self.time_stop_id)
                self.time_stop_id = None
            if self.time_start_id is None:
                self.time_start_id = self.startTimer(1000)

        if self.stop and not self.start:
            if self.time_start_id is not None:
                self.killTimer(self.time_start_id)
                self.time_start_id = None
            if self.time_stop_id is None:
                self.time_stop_id = self.startTimer(1000)

    def timerEvent(self, event):
        print(event.timerId())
        if event.timerId()==self.time_start_id:
            print("start id is here")
        if event.timerId() == self.time_stop_id:
            print("stop id is here")



from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *



class Ui(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(600,600)
        self.btn = QPushButton("btn1",self)
        self.btn2 = QPushButton("btn2",self)
        self.btn.move(100,50)
        self.btn2.move(100,150)

        self.btn.clicked.connect(self.btn_click)
        self.btn2.clicked.connect(self.btn_click2)

        self.motor = Motors()

    def btn_click(self):

        self.motor.start = 1
        self.motor.stop = 0
        self.motor.run()
    def btn_click2(self):
        self.motor.start = 0
        self.motor.stop = 1
        self.motor.run()


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    win = Ui()
    win.show()

    sys.exit(app.exec())

问题得以解决

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值