Threading and Signals problem in PyQt

2024/9/20 14:48:17

I'm having some problems with communicating between Threads in PyQt. I'm using signals to communicate between two threads, a Sender and a Listener. The sender sends messages, which are expected to be received by the listener. However, no messages are receieved. Can anyone suggest what might be going wrong? I'm sure it must be something simple, but I've been looking around for hours and not found anything. Thanks in advance!

from PyQt4 import QtCore,QtGui
import timeclass Listener(QtCore.QThread):    def __init__(self):super(Listener,self).__init__()def run(self):# just stay alive, waiting for messagesprint 'Listener started'while True:print '...'time.sleep(2)def say_hello(self):print ' --> Receiver: Hello World!'class Sender(QtCore.QThread):# a signal with no argumentssignal = QtCore.pyqtSignal()def __init__(self):super(Sender,self).__init__()# create and start a listenerself.listener = Listener()self.listener.start()# connect up the signalself.signal.connect(self.listener.say_hello)# start this threadself.start()def run(self):print 'Sender starting'# send five signalsfor i in range(5):print 'Sender -->'self.signal.emit()time.sleep(2)# the sender's work is doneprint 'Sender finished'
Answer

I'm not sure if that is what you need, but it works fine...

from PyQt4 import QtCore,QtGui
import timeclass Listener(QtCore.QThread):def __init__(self):super(Listener,self).__init__()def run(self):print('listener: started')while True:time.sleep(2)def connect_slots(self, sender):self.connect(sender, QtCore.SIGNAL('testsignal'), self.say_hello)def say_hello(self):print('listener: received signal')class Sender(QtCore.QThread):def __init__(self):super(Sender,self).__init__()def run(self):for i in range(5):print('sender: sending signal')self.emit(QtCore.SIGNAL('testsignal'))time.sleep(2)print('sender: finished')if __name__ == '__main__':o_qapplication = QtGui.QApplication([])my_listener = Listener()my_sender = Sender()my_listener.connect_slots(my_sender)my_listener.start()my_sender.start()i_out = o_qapplication.exec_()
https://en.xdnf.cn/q/72162.html

Related Q&A

stopping a python thread using __del__

I have a threaded program in Python that works fine except that __del__ does not get called once the thread is running:class tt(threading.Thread):def __init__(self):threading.Thread.__init__(self)self.…

Python-docx: Is it possible to add a new run to paragraph in a specific place (not at the end)

I want to set a style to a corrected word in MS Word text. Since its not possible to change text style inside a run, I want to insert a new run with new style into the existing paragraph...for p in doc…

Chained QSortFilterProxyModels

Lets say I have a list variable datalist storing 10,000 string entities. The QTableView needs to display only some of these entities. Thats is why QTableView was assigned QSortFilterProxyModel that doe…

Python subprocess.popen() without waiting

Im using Python 3.4.2 on Windows. In script1.py Im doing this:myProc = subprocess.Popen([sys.executable, "script2.py", "argument"]) myProc.communicate()it works and call script2.py …

Python27.dll File Missing - Exception

I have downloaded my code from bit-bucket which was made by my group member. It contain all the frameworks and python script folder. But when I run this code on my system it generates the following err…

Download an Image Using Selenium Webdriver in Python

I am trying to download an image from a URL using Selenium Webdriver in Python. The site is protected by a login page, so cant just save the URL contents using requests. I am able to get text from the …

Can I turn off implicit Python unicode conversions to find my mixed-strings bugs?

When profiling our code I was surprised to find millions of calls toC:\Python26\lib\encodings\utf_8.py:15(decode)I started debugging and found that across our code base there are many small bugs, usual…

jupyter: how to stop execution on errors?

The common way to defensively abort execution in python is to simply do something like: if something_went_wrong:print("Error message: goodbye cruel world")exit(1)However, this is not good pra…

Python 2.7 on Google App Engine, cannot use lxml.etree

Ive been trying to use html5lib with lxml on python 2.7 in google app engine. But when I run the following code, it gives me an error saying "NameError: global name etree is not defined". Is …

Pandas split name column into first and last name if contains one space

Lets say I have a pandas DataFrame containing names like so:name_df = pd.DataFrame({name:[Jack Fine,Kim Q. Danger,Jane Smith, Juan de la Cruz]})name 0 Jack Fine 1 Kim Q. Danger 2 Jane Smith 3 J…