473,574 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PyQt: Is signal / slot really working across threads?

Hello pyqt users,

i tried to use signal / slot across threads. With the following example I want
to emit a signal when the thread loop is entered. The connected slot is never
called. Why?

Any help is very welcome ...

Alexander

import time
import sys
import PyQt4
from PyQt4.QtCore import (QObject, QThread)
SIGNAL = PyQt4.QtCore.SI GNAL

class CancelableQtThr ead_(QThread):

def __init__(self):
QThread.__init_ _(self)
self.sigStarted = SIGNAL("sigStar ted()")

def run(self):
print "Enter thread"
self.emit(self. sigStarted)
time.sleep(0.1)
print "Leave thread"

class TestSigSlot(QOb ject):

def __init__(self):
QObject.__init_ _(self)
self._thread = CancelableQtThr ead_()
self.connect(se lf._thread, self._thread.si gStarted, self.Called)
self._thread.st art()

time.sleep(1.0)

def Called(self):
print "Called !"

if __name__ == "__main__":
obj = TestSigSlot()
May 29 '07 #1
2 3704
On Tuesday 29 May 2007 11:58 am, Alexander Eisenhuth wrote:
Hello pyqt users,

i tried to use signal / slot across threads. With the following example I
want to emit a signal when the thread loop is entered. The connected slot
is never called. Why?

Any help is very welcome ...

Alexander

import time
import sys
import PyQt4
from PyQt4.QtCore import (QObject, QThread)
SIGNAL = PyQt4.QtCore.SI GNAL

class CancelableQtThr ead_(QThread):

def __init__(self):
QThread.__init_ _(self)
self.sigStarted = SIGNAL("sigStar ted()")

def run(self):
print "Enter thread"
self.emit(self. sigStarted)
time.sleep(0.1)
print "Leave thread"

class TestSigSlot(QOb ject):

def __init__(self):
QObject.__init_ _(self)
self._thread = CancelableQtThr ead_()
self.connect(se lf._thread, self._thread.si gStarted, self.Called)
self._thread.st art()

time.sleep(1.0)

def Called(self):
print "Called !"

if __name__ == "__main__":
obj = TestSigSlot()
Signals across threads are implemented using the event loop. You don't have an
event loop running in your main thread - you don't even have a
QCoreApplicatio n instance.

Phil
May 29 '07 #2
Ok, thanks.

Phil Thompson schrieb:
On Tuesday 29 May 2007 11:58 am, Alexander Eisenhuth wrote:
>Hello pyqt users,

i tried to use signal / slot across threads. With the following example I
want to emit a signal when the thread loop is entered. The connected slot
is never called. Why?

Any help is very welcome ...

Alexander

import time
import sys
import PyQt4
from PyQt4.QtCore import (QObject, QThread)
SIGNAL = PyQt4.QtCore.SI GNAL

class CancelableQtThr ead_(QThread):

def __init__(self):
QThread.__init_ _(self)
self.sigStarted = SIGNAL("sigStar ted()")

def run(self):
print "Enter thread"
self.emit(self. sigStarted)
time.sleep(0.1)
print "Leave thread"

class TestSigSlot(QOb ject):

def __init__(self):
QObject.__init_ _(self)
self._thread = CancelableQtThr ead_()
self.connect(se lf._thread, self._thread.si gStarted, self.Called)
self._thread.st art()

time.sleep(1.0)

def Called(self):
print "Called !"

if __name__ == "__main__":
obj = TestSigSlot()

Signals across threads are implemented using the event loop. You don't have an
event loop running in your main thread - you don't even have a
QCoreApplicatio n instance.

Phil
May 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3557
by: Sibylle Koczian | last post by:
Still trying to learn PyQt from a book about several Python GUI toolkits, I seem to learn first what doesn't work. The following small script seems to work, but after closing the window I get the error message "Fatal Python error: PyEval_RestoreThread: NULL tstate". Without the line "self.app = app" the error goes away. So I suppose the...
10
11690
by: Peter | last post by:
I want to draw some lines on a widget. This works ok, but when I want to redraw, the old lines are still there. How do I clear or refresh the widget, so I can draw a new set of lines? Code snip below. TIA Peter
3
3063
by: Marcpp | last post by:
Hi, I'm introducing to program in python + pyqt. I have a main window that call a second window (to introduce a info with textedit) when press the second window button I need to return to the main window the info introduced in the second window. I've seek in the pyqt doc examples but i don't find it. Have you any example?
7
3216
by: Alex Gusarov | last post by:
Hello, I have strong .NET background with C# and want to do some familiar things from it with Python, but don't know how. For example, I created form in qt designer with QCalendarWidget, translated it into Python module and want to overload virtual method paintCell of QCalendarWidget. In C# I can write following (abstract) code: ...
0
7807
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8058
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7820
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5301
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2249
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1061
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.