Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to emit cross-thread signal in Qt?
    primarykey
    data
    text
    <p>Qt documentation states that signals and slots can be <code>direct</code>, <code>queued</code> and <code>auto</code>. </p> <p>It also stated that if object that owns slot 'lives' in a thread different from object that owns signal, emitting such signal will be like posting message - signal emit will return instantly and slot method will be called in target thread's event loop. </p> <p>Unfortunately, documentation do not specify that 'lives' stands for and no examples is available. I have tried the following code:</p> <p><strong><code>main.h:</code></strong></p> <pre><code>class CThread1 : public QThread { Q_OBJECT public: void run( void ) { msleep( 200 ); std::cout &lt;&lt; "thread 1 started" &lt;&lt; std::endl; MySignal(); exec(); } signals: void MySignal( void ); }; class CThread2 : public QThread { Q_OBJECT public: void run( void ) { std::cout &lt;&lt; "thread 2 started" &lt;&lt; std::endl; exec(); } public slots: void MySlot( void ) { std::cout &lt;&lt; "slot called" &lt;&lt; std::endl; } }; </code></pre> <p><strong><code>main.cpp:</code></strong></p> <pre><code>int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); CThread1 oThread1; CThread2 oThread2; QObject::connect( &amp; oThread1, SIGNAL( MySignal() ), &amp; oThread2, SLOT( MySlot() ) ); oThread1.start(); oThread2.start(); oThread1.wait(); oThread2.wait(); return a.exec(); } </code></pre> <p>Output is:</p> <pre><code>thread 2 started thread 1 started </code></pre> <p><code>MySlot()</code> is never called :(. What I'm doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload