472,143 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

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.SIGNAL

class CancelableQtThread_(QThread):

def __init__(self):
QThread.__init__(self)
self.sigStarted = SIGNAL("sigStarted()")

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

class TestSigSlot(QObject):

def __init__(self):
QObject.__init__(self)
self._thread = CancelableQtThread_()
self.connect(self._thread, self._thread.sigStarted, self.Called)
self._thread.start()

time.sleep(1.0)

def Called(self):
print "Called !"

if __name__ == "__main__":
obj = TestSigSlot()
May 29 '07 #1
2 3581
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.SIGNAL

class CancelableQtThread_(QThread):

def __init__(self):
QThread.__init__(self)
self.sigStarted = SIGNAL("sigStarted()")

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

class TestSigSlot(QObject):

def __init__(self):
QObject.__init__(self)
self._thread = CancelableQtThread_()
self.connect(self._thread, self._thread.sigStarted, self.Called)
self._thread.start()

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
QCoreApplication 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.SIGNAL

class CancelableQtThread_(QThread):

def __init__(self):
QThread.__init__(self)
self.sigStarted = SIGNAL("sigStarted()")

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

class TestSigSlot(QObject):

def __init__(self):
QObject.__init__(self)
self._thread = CancelableQtThread_()
self.connect(self._thread, self._thread.sigStarted, self.Called)
self._thread.start()

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
QCoreApplication instance.

Phil
May 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

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.