PyQt: Is signal / slot really working across threads?

Discussion in 'Python' started by Alexander Eisenhuth, May 29, 2007.

  1. 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()
     
    Alexander Eisenhuth, May 29, 2007
    #1
    1. Advertising

  2. 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
     
    Phil Thompson, May 29, 2007
    #2
    1. Advertising

  3. 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
     
    Alexander Eisenhuth, May 29, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pradnyesh Sawant

    problem with qthread and signal/slot

    Pradnyesh Sawant, Apr 14, 2007, in forum: Python
    Replies:
    0
    Views:
    477
    Pradnyesh Sawant
    Apr 14, 2007
  2. Pradnyesh Sawant

    passing tuple with pyqt4 signal/slot mechanism

    Pradnyesh Sawant, Apr 26, 2007, in forum: Python
    Replies:
    2
    Views:
    341
    Phil Thompson
    Apr 26, 2007
  3. Pradnyesh Sawant

    pyqt4 signal/slot using PyObject* and shortcut

    Pradnyesh Sawant, Apr 27, 2007, in forum: Python
    Replies:
    0
    Views:
    328
    Pradnyesh Sawant
    Apr 27, 2007
  4. Alexander Eisenhuth

    PyQt disconnect signal / slot

    Alexander Eisenhuth, Sep 25, 2007, in forum: Python
    Replies:
    2
    Views:
    1,309
    Alexander Eisenhuth
    Sep 25, 2007
  5. Steven Woody

    Event Handling and Signal-Slot Mechanism

    Steven Woody, Jan 19, 2009, in forum: Python
    Replies:
    2
    Views:
    1,091
    Steven Woody
    Jan 20, 2009
Loading...

Share This Page