Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are quite a few problems with your code :</p> <ul> <li>like said by Evan the emit keyword is missing</li> <li>all your objects live in the main thread, only the code in the run methods live in other threads, which means that the MySlot slot would be called in the main thread and I'm not sure that's what you want</li> <li>your slot will never be called since the main event loop will never been launched : your two calls to wait() will only timeout after a very long time (and you'll probably kill your application before that happens) and I don't think that's what you want either, anyway they really have no use in your code.</li> </ul> <p>This code would most likely work (though I have not tested it) and I think it does what you want it to do :</p> <pre><code>class MyObject : public QObject { Q_OBJECT public slots: void MySlot( void ) { std::cout &lt;&lt; "slot called" &lt;&lt; std::endl; } }; class CThread1 : public QThread { Q_OBJECT public: void run( void ) { std::cout &lt;&lt; "thread 1 started" &lt;&lt; std::endl; int i = 0; while(1) { msleep( 200 ); i++; if(i==1000) emit MySignal(); } } 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(); } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); CThread1 oThread1; CThread2 oThread2; MyObject myObject; QObject::connect( &amp; oThread1, SIGNAL( MySignal() ), &amp; myObject, SLOT( MySlot() ) ); oThread2.start(); myObject.moveToThread(&amp;oThread2) oThread1.start(); return a.exec(); } </code></pre> <p>Now MyObject will live in thread2 (thanks to moveToThread).</p> <p>MySignal should be sent from thread1 (thought I'm not sure on that one, it might be sent from main thread, it doesn't really matter).</p> <p>No event loop is needed in thread1 since emitting a signal doesn't need an event loop. An event loop is needed in thread2 (lanched by exec()) to receive the signal.</p> <p>MySlot will be called in thread2.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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