Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PySide中的信号在被计时器调用时不发出_Python_Qt_Pyside - Fatal编程技术网

Python PySide中的信号在被计时器调用时不发出

Python PySide中的信号在被计时器调用时不发出,python,qt,pyside,Python,Qt,Pyside,我需要定期发出信号。定时器执行特定的功能,发出我想要的信号。由于某些原因,此函数未被发出。我能够在最少的代码中重现错误(请参阅下文)。如果我在没有计时器的情况下也这样做,一切都会正常工作: from threading import Timer import time from PySide import QtCore class testSignals(QtCore.QObject): signal = QtCore.Signal(); def __init__(self):

我需要定期发出信号。定时器执行特定的功能,发出我想要的信号。由于某些原因,此函数未被发出。我能够在最少的代码中重现错误(请参阅下文)。如果我在没有计时器的情况下也这样做,一切都会正常工作:

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()
我能够用以下代码重现我的问题:

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

“接收到信号”一词不会在屏幕上打印出来

正如@ekhumaro在评论中所说,QTimer帮了大忙。我需要一个事件循环。我发布代码是为了让答案有效。我用QTimer重写了它,这让一切变得更简单。注意,我需要调用QCoreApplication来运行线程。同样,这只是复制我需要做的事情的最小代码

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

没有事件循环信号就不能工作。你能详细说明一下吗?我认为这是因为线程不在Qt的控制范围内,但我还没有找到解决方法。我将介绍一下:对于直接信号/插槽连接,不需要事件循环(甚至不需要应用程序对象)。但是对于跨线程连接,信号被发布到事件队列并进行异步处理。当然,您可能需要使用
QThread
来实现这一点,尽管我认为可能会有一些变通方法(不要问我如何)。但不管怎样:为什么不使用
QTimer