Note that there are some explanatory texts on larger screens.

plurals
  1. POQt signaling across threads, one is GUI thread?
    text
    copied!<p>What does it mean to move a object from one thread to another in Qt using moveToThread? Everything seems to work even before using moveToThread, which moves the object from one thread (GUI thread) to a another thread ( worked) and Qt:connect calls the appropriate slot on object.</p> <p>Is there any difference because of where the object lives, GUI thread or the worker thread?</p> <p>EDIT: I made a small program, but I don't understand how QThread works along with Signal and slot function, I would appreciate if you could explain what is the use of moveToThread with the example</p> <pre><code>#include &lt;QtGui/QApplication&gt; #include &lt;QPushButton&gt; #include &lt;QHBoxLayout&gt; #include &lt;QLineEdit&gt; #include &lt;QString&gt; #include "mythread.h" //GUI calls a thread to do some job and sub update the text box once it is done int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget w; QHBoxLayout * pH = new QHBoxLayout(&amp;w); QPushButton * pushButton = new QPushButton("asdad"); QLineEdit * lineEdit = new QLineEdit("AAA"); pH-&gt;addWidget(pushButton); pH-&gt;addWidget(lineEdit); w.setLayout(pH); w.show(); MyThread thread; qDebug("Thread id %d",(int)QThread::currentThreadId()); QObject::connect(pushButton,SIGNAL(clicked()),&amp;thread,SLOT(callRun())) ; QObject::connect(&amp;thread,SIGNAL(signalGUI(QString)),lineEdit,SLOT(setText(QString))); return a.exec(); } #ifndef MYTHREAD_H #define MYTHREAD_H #include &lt;QThread&gt; #include &lt;QMutex&gt; class MyThread : public QThread { Q_OBJECT public: MyThread(); public slots: void callRun(); void run(); signals: void signalGUI(QString); private: QMutex mutex; }; #endif // MYTHREAD_H #include "mythread.h" #include &lt;QDebug&gt; #include &lt;QString&gt; #include &lt;QMutexLocker&gt; MyThread::MyThread() { } void MyThread::callRun() { qDebug("in thread"); if(!isRunning()) { this-&gt;start(LowestPriority); exec(); } else { run(); } } void MyThread::run() { QMutexLocker fn_scope(&amp;mutex); static int a = 0; ++a; qDebug("Thread id inside run %d",(int)QThread::currentThreadId()); this-&gt;sleep(3); static QString number; QString temp; number += temp.setNum(a); emit signalGUI(number); } </code></pre>
 

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