Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take a look at <a href="https://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads" rel="nofollow noreferrer">Signals and slots across threads</a>. If you always use signals and slots to communicate with the worker thread, Qt handles the moveToThread for you if it's needed and you used the correct connection.</p> <p>Edit: I would guess the article's author was seeing his problem since he was calling start in the constructor before the thread was actually created. In other words, don't trust third-party code blindly.</p> <p>Edit: In response to your comment, look at the <a href="https://doc.qt.io/qt-5/qtcore-threads-mandelbrot-example.html" rel="nofollow noreferrer">Mandelbrot</a> example, under the <code>MandelbrotWidget Class Implementation</code> header:</p> <blockquote> <p>With queued connections, Qt must store a copy of the arguments that were passed to the signal so that it can pass them to the slot later on. Qt knows how to take of copy of many C++ and Qt types, but QImage isn't one of them. We must therefore call the template function qRegisterMetaType() before we can use QImage as parameter in queued connections.</p> </blockquote> <p>I believe this is slightly outdated, here are the <a href="https://doc.qt.io/qt-5/qmetatype.html#Type-enum" rel="nofollow noreferrer">valid meta types</a>. Since signals and slots across threads use queued connections, you should not have to do the moveToThread calls in most cases.</p> <p>Edit: I will try to explain things with a similar example:</p> <p>mythread.h:</p> <pre><code>#ifndef MYTHREAD_H #define MYTHREAD_H #include &lt;QThread&gt; #include &lt;QMutex&gt; class MyThread : public QThread { Q_OBJECT protected: virtual void run(); signals: void signalGUI(QString); }; #endif // MYTHREAD_H </code></pre> <p>mythread.cpp:</p> <pre><code>#include "mythread.h" #include &lt;QString&gt; void MyThread::run() { qDebug("Thread id inside run %d",(int)QThread::currentThreadId()); static int run = 0; QString temp = QString("Run: %1").arg(run++); qDebug("String address inside run %p", &amp;temp); emit signalGUI(temp); } </code></pre> <p>mylineedit.h</p> <pre><code>#ifndef MYLINEEDIT_H #define MYLINEEDIT_H #include &lt;QLineEdit&gt; class MyLineEdit : public QLineEdit { Q_OBJECT public: explicit MyLineEdit(QWidget *parent = 0); public slots: void setText(const QString &amp;string); }; #endif // MYLINEEDIT_H </code></pre> <p>mylineedit.cpp</p> <pre><code>#include "mylineedit.h" #include &lt;QThread&gt; MyLineEdit::MyLineEdit(QWidget *parent) : QLineEdit(parent) { } void MyLineEdit::setText(const QString &amp;string) { qDebug("Thread id inside setText %d",(int)QThread::currentThreadId()); qDebug("String address inside setText %p\n", &amp;string); QLineEdit::setText(string); } </code></pre> <p>main.cpp:</p> <pre><code>#include &lt;QApplication&gt; #include &lt;QPushButton&gt; #include &lt;QHBoxLayout&gt; #include "mythread.h" #include "mylineedit.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("Run Thread", &amp;w); MyLineEdit * lineEdit = new MyLineEdit(&amp;w); pH-&gt;addWidget(pushButton); pH-&gt;addWidget(lineEdit); w.show(); MyThread thread; qDebug("Thread id %d",(int)QThread::currentThreadId()); QObject::connect(pushButton,SIGNAL(clicked()),&amp;thread,SLOT(start())) ; QObject::connect(&amp;thread,SIGNAL(signalGUI(const QString&amp;)),lineEdit,SLOT(setText(const QString&amp;))); return a.exec(); } </code></pre> <p>Sample output after clicking button:</p> <pre>Thread id 1088110320 Thread id inside run 1093176208 String address inside run 0x41288350 Thread id inside setText 1088110320 String address inside setText 0x974af58</pre> <p>As you can see, the run thread is different than the main GUI thread. Also, even though you pass a const reference to a QString, since it crosses thread boundaries it copies it. I <strong>strongly</strong> encourage you to read <a href="https://doc.qt.io/qt-5/threads-qobject.html" rel="nofollow noreferrer">Threads and QObject</a>.</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