计时器调用时未发出 PySide 中的信号 [英] Signal in PySide not emitted when called by a timer

查看:75
本文介绍了计时器调用时未发出 PySide 中的信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定期发出信号.计时器执行某些功能,该功能会发出我想要的信号.由于某种原因,这个函数没有被发出.我能够在最少的代码上重现错误(请参见下文).如果我在没有计时器的情况下做同样的事情,一切正常:

I need to emit a signal periodically. A timer executes certain function, which emits the signal that I want. For some reason this function is not being emitted. I was able to reproduce the error on minimal code (see further down). If I do the same without the timer everything works:

from threading import Timer
import time
from PySide import QtCore

class testSignals(QtCore.QObject):
    signal = QtCore.Signal();
    def __init__(self):
        QtCore.QObject.__init__(self)

    def run(self):
        self.signal.emit()

class testConnection():
    @QtCore.Slot()
    def receiveMe(self):
        print('Signal received')

    # signal is emmited when data is changed
    def __init__(self):
        test = testSignals()
        test.signal.connect(self.receiveMe)
        test.run();

test = testConnection()

我能够在以下代码中重现我的问题:

I was able to reproduce my problem in the following code:

from threading import Timer
import time
from PySide import QtCore

class testSignals(QtCore.QObject):

    signal = QtCore.Signal();

    def __init__(self):
        self.is_running = False
        QtCore.QObject.__init__(self)
        self.start()

    def emitMe(self):
        self.is_running = False
        self.start()
        # print("emitMe")
        self.signal.emit()

    def start(self):
        if not self.is_running:
            self._timer = Timer(0.2, self.emitMe)
            self._timer.start()
            self.is_running = True

    def stop(self):
        self._timer.cancel()
        self.is_running = False

class testConnection():
    @QtCore.Slot()
    def receiveMe(self):
        print('Signal received')

    # signal is emmited when data is changed
    def __init__(self):
        test = testSignals()
        test.signal.connect(self.receiveMe)
        test.start();
        time.sleep(2.0)
        test.stop();

test = testConnection()

屏幕上没有打印收到信号"这句话.

the phrase "Signal received" does not get printed on the screen.

推荐答案

正如@ekhumoro 在评论中所说,QTimer 帮助很大.我需要一个事件循环.我正在发布代码以使答案有效.我用 QTimer 重写了它,这让一切都变得更简单了.请注意,我需要调用 QCoreApplication 来运行线程.同样,这只是重现我需要做的事情的最少代码.

As @ekhumoro said in the comments, QTimer helped a lot. I needed an event-loop. I'm posting the code to make the answer valid. I rewrote it using QTimer, which made everything simpler really. Note that I need to call QCoreApplication to run the threads. Again, this is just the minimal code to reproduce what I needed to do.

import time
from PySide import QtCore
from probeConnection import ProbeConnection 

class testSignals(QtCore.QObject):

    def __init__(self):
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.tick)
        self.start()

    def tick(self):
        print("tick")

    def getData(self):
        return time.strftime('%H:%M:%S')

    def start(self):
        self.timer.start(1000)

    def stop(self):
        self.timer.stop()

class testConnection():
    def receiveMe(self):
        print('time: ' + self.test.getData())

    def __init__(self):
        self.test = testSignals()
        self.test.timer.timeout.connect(self.receiveMe)


app = QtCore.QCoreApplication([])
test = testConnection()
timer = QtCore.QTimer()
timer.singleShot(4000,app.exit)
app.exec_()

它产生:

tick
time: 13:23:37
tick
time: 13:23:38
tick
time: 13:23:39
tick
time: 13:23:40

这篇关于计时器调用时未发出 PySide 中的信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆