Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 571531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:31:01+00:00 2026-05-13T13:31:01+00:00

What does it mean to move a object from one thread to another in

  • 0

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.

Is there any difference because of where the object lives, GUI thread or the worker thread?

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

#include <QtGui/QApplication>
#include <QPushButton>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QString>
#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(&w);
    QPushButton * pushButton = new QPushButton("asdad");
    QLineEdit * lineEdit = new QLineEdit("AAA");
    pH->addWidget(pushButton);
    pH->addWidget(lineEdit);
    w.setLayout(pH);
    w.show();
    MyThread thread;
    qDebug("Thread id %d",(int)QThread::currentThreadId());
    QObject::connect(pushButton,SIGNAL(clicked()),&thread,SLOT(callRun())) ;
    QObject::connect(&thread,SIGNAL(signalGUI(QString)),lineEdit,SLOT(setText(QString)));
    return a.exec();
}

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QMutex>

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 <QDebug>
#include <QString>
#include <QMutexLocker>

MyThread::MyThread()
{
}
 void MyThread::callRun()
 {

     qDebug("in thread");
    if(!isRunning())
     {
        this->start(LowestPriority);
        exec();
    }
    else
    {
        run();
    }

 }
 void MyThread::run()
 {
     QMutexLocker fn_scope(&mutex);
     static int a = 0;
    ++a;
     qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
     this->sleep(3);
     static QString number;
     QString temp;
     number += temp.setNum(a);
     emit signalGUI(number);
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:31:02+00:00Added an answer on May 13, 2026 at 1:31 pm

    Take a look at Signals and slots across threads. 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.

    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.

    Edit: In response to your comment, look at the Mandelbrot example, under the MandelbrotWidget Class Implementation header:

    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.

    I believe this is slightly outdated, here are the valid meta types. Since signals and slots across threads use queued connections, you should not have to do the moveToThread calls in most cases.

    Edit:
    I will try to explain things with a similar example:

    mythread.h:

    #ifndef MYTHREAD_H
    #define MYTHREAD_H
    
    #include <QThread>
    #include <QMutex>
    
    class MyThread : public QThread
    {
       Q_OBJECT
    
    protected:
       virtual void run();
    
    signals:
       void signalGUI(QString);
    };
    
    #endif // MYTHREAD_H
    

    mythread.cpp:

    #include "mythread.h"
    #include <QString>
    
    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", &temp);
       emit signalGUI(temp);
    }
    

    mylineedit.h

    #ifndef MYLINEEDIT_H
    #define MYLINEEDIT_H
    
    #include <QLineEdit>
    
    class MyLineEdit : public QLineEdit
    {
    Q_OBJECT
    public:
        explicit MyLineEdit(QWidget *parent = 0);
    
    public slots:
        void setText(const QString &string);
    
    };
    
    #endif // MYLINEEDIT_H
    

    mylineedit.cpp

    #include "mylineedit.h"
    #include <QThread>
    
    MyLineEdit::MyLineEdit(QWidget *parent) :
        QLineEdit(parent)
    {
    }
    
    void MyLineEdit::setText(const QString &string)
    {
       qDebug("Thread id inside setText %d",(int)QThread::currentThreadId());
       qDebug("String address inside setText %p\n", &string);
       QLineEdit::setText(string);
    }
    

    main.cpp:

    #include <QApplication>
    #include <QPushButton>
    #include <QHBoxLayout>
    #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(&w);
       QPushButton * pushButton = new QPushButton("Run Thread", &w);
       MyLineEdit * lineEdit = new MyLineEdit(&w);
    
       pH->addWidget(pushButton);
       pH->addWidget(lineEdit);
       w.show();
    
       MyThread thread;
       qDebug("Thread id %d",(int)QThread::currentThreadId());
       QObject::connect(pushButton,SIGNAL(clicked()),&thread,SLOT(start())) ;
       QObject::connect(&thread,SIGNAL(signalGUI(const QString&)),lineEdit,SLOT(setText(const QString&)));
       return a.exec();
    }
    

    Sample output after clicking button:

    Thread id 1088110320
    Thread id inside run 1093176208
    String address inside run 0x41288350
    Thread id inside setText 1088110320
    String address inside setText 0x974af58

    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 strongly encourage you to read Threads and QObject.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 482k
  • Answers 482k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, it isn't an overrun - after all, this can… May 16, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer You can set useVirtualLayout on the List to false, which… May 16, 2026 at 6:52 am
  • Editorial Team
    Editorial Team added an answer This is actually a very popular problem, and is called… May 16, 2026 at 6:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.