The Qt documentation says that signals and slots can be direct , queued and auto .
He also stated that if an object that owns a "life" slot in a stream other than an object that owns the signal emits such a signal, it will be like a send message - the emit signal will return instantly, and the slot method will be called in the target loop of the stream's events ,
Unfortunately, the documentation does not indicate what βlifeβ means, and there are no examples. I tried the following code:
main.h:
class CThread1 : public QThread { Q_OBJECT public: void run( void ) { msleep( 200 ); std::cout << "thread 1 started" << std::endl; MySignal(); exec(); } signals: void MySignal( void ); }; class CThread2 : public QThread { Q_OBJECT public: void run( void ) { std::cout << "thread 2 started" << std::endl; exec(); } public slots: void MySlot( void ) { std::cout << "slot called" << std::endl; } };
main.cpp:
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); CThread1 oThread1; CThread2 oThread2; QObject::connect( & oThread1, SIGNAL( MySignal() ), & oThread2, SLOT( MySlot() ) ); oThread1.start(); oThread2.start(); oThread1.wait(); oThread2.wait(); return a.exec(); }
Exit:
thread 2 started thread 1 started
MySlot() never called :( What am I doing wrong?
c ++ qt signals signals-slots qt-signals
grigoryvp Mar 12 '09 at 11:38 2009-03-12 11:38
source share