PyQt: Is signal / slot really working across threads?

  • Thread starter Alexander Eisenhuth
  • Start date
A

Alexander Eisenhuth

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

Phil Thompson

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
 
A

Alexander Eisenhuth

Ok, thanks.

Phil said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top