Signal in PySide not emitted when called by a timer

2024/10/12 4:30:00

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 QtCoreclass 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 changeddef __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 QtCoreclass testSignals(QtCore.QObject):signal = QtCore.Signal();def __init__(self):self.is_running = FalseQtCore.QObject.__init__(self)self.start()def emitMe(self):self.is_running = Falseself.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 = Truedef stop(self):self._timer.cancel()self.is_running = Falseclass testConnection():@QtCore.Slot()def receiveMe(self):print('Signal received')# signal is emmited when data is changeddef __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.

Answer

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

it produces:

tick
time: 13:23:37
tick
time: 13:23:38
tick
time: 13:23:39
tick
time: 13:23:40
https://en.xdnf.cn/q/118239.html

Related Q&A

pybuilder and pytest: cannot import source code when running tests

so i have a project:<root> |- src|-main|-python|-data_merger|- common|- constans|- controller|- resources|- rest|-tests|-unittest|-integrationtestdata_merger is marked as root (I am using Pycharm…

HTTPS proxy server python

I have a problem with my ssl server (in Python). I set the SSL proxy connection in my browser, and try to connect to my ssl server.This is the server:import BaseHTTPServer, SimpleHTTPServer import sslh…

Python 2.7.6 + unicode_literals - UnicodeDecodeError: ascii codec cant decode byte

Im trying to print the following unicode string but Im receiving a UnicodeDecodeError: ascii codec cant decode byte error. Can you please help form this query so it can print the unicode string properl…

Retrieving data from Quandl with Python

How can I get the latest prices from a Quandl dataset with the Python API (https://www.quandl.com/help/python)? On https://www.quandl.com/help/api, it says "You can use rows=n to get only the fir…

Django: Using same object how to show 2 different results in django template?

Using the same object how to SHOW 2 different results using django template ?In one page there are two divs, it should show different information using the same object.INPUTobject data has follows[{&q…

Override attribute access precedence having a data descriptor

I have a bunch of instances of a MongoEngine model. And the profiler shows that a lot of time is spent in __get__ method of MongoEngine model fields:ncalls tottime percall cumtime percall filename:…

Understanding pythons reverse slice ( [::-1] )

I always thought that omitting arguments in the python slice operation would result into:start = 0 end = len(lst) step = 1That holds true if the step is positive, but as soon as the step is negative, l…

How to print list elements (which are also lists) in separated lines in Python

Ive checked the post and answers on the SO post Printing list elements on separated lines in Python, while I think my problem is a different one.What I want is to transform:lsts = [[1], [1, 1], [1, 2, …

Python3 threading, trying to ping multiple IPs/test port simultaineously

Full (non-working) code belowFull (working, w/o threading) code here: http://pastebin.com/KUYzNtT2Ive written a small script that does the following:Pull network information from a database Ping each I…

How to print a list of numbers without square brackets?

Im generating a list of random digits, but Im struggling to figure out how to output the digits in a single row without the square brackets?import random def generateAnswer(answerList):listofDigits =…