Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot create children for a parent that is in a different thread
QtWS25 Last Chance

Cannot create children for a parent that is in a different thread

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 5.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bruschetta
    wrote on last edited by
    #1

    I have a thread that is "listening" for some parameters and a widget that want to "consume" and elaborate those to show some resoult on the screen.This scenario is called after button click.

    The code i'm trying to execute is this:

    void Example::on_pushButton_clicked()
    {
        dialog *dia = new dialog();
        GetterThread *thread = new GetterThread;
        thread->start();
        dialog->exec();
    
        connect( thread,  SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection );
    }
    

    During the execution of the code i get this error:
    QObject: Cannot create children for a parent that is in a different thread.

    The problem is that Dialog is running on the GUI Thread and Elaborate is on the GetterThread.

    So I have tryied to add this line of code but is not working because dialog is a Widget...
    QObject: Cannot create children for a parent that is in a different thread.

        dialog->moveToThread(thread);
    

    Can someone gie me some hints to understand what i'm doing wrong?

    jsulmJ 1 Reply Last reply
    0
    • joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      From your code

      connect( thread, SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection );
      }

      And you said

      Elaborate is on the GetterThread ??? are you true ?

      connect( thread,  SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection ); // dia is the slot's class ? 
      

      Qt manual

      QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)

      Note:

      1. sender and sender's signal;
      2. receiver and receiver's slot;

      Just do it!

      B 1 Reply Last reply
      0
      • joeQJ joeQ

        From your code

        connect( thread, SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection );
        }

        And you said

        Elaborate is on the GetterThread ??? are you true ?

        connect( thread,  SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection ); // dia is the slot's class ? 
        

        Qt manual

        QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)

        Note:

        1. sender and sender's signal;
        2. receiver and receiver's slot;
        B Offline
        B Offline
        Bruschetta
        wrote on last edited by
        #3

        @joeQ said in Cannot create children for a parent that is in a different thread:

        And you said

        Elaborate is on the GetterThread ??? are you true ?

        Well is obviously a misstype, i inverted the GUI Thread And the GetterThread.
        There is really no need to be rude.
        I'm here to learn and understand what i'm doing wrong.

        Yes dia is the SLOT class.

        joeQJ 1 Reply Last reply
        0
        • B Bruschetta

          I have a thread that is "listening" for some parameters and a widget that want to "consume" and elaborate those to show some resoult on the screen.This scenario is called after button click.

          The code i'm trying to execute is this:

          void Example::on_pushButton_clicked()
          {
              dialog *dia = new dialog();
              GetterThread *thread = new GetterThread;
              thread->start();
              dialog->exec();
          
              connect( thread,  SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection );
          }
          

          During the execution of the code i get this error:
          QObject: Cannot create children for a parent that is in a different thread.

          The problem is that Dialog is running on the GUI Thread and Elaborate is on the GetterThread.

          So I have tryied to add this line of code but is not working because dialog is a Widget...
          QObject: Cannot create children for a parent that is in a different thread.

              dialog->moveToThread(thread);
          

          Can someone gie me some hints to understand what i'm doing wrong?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Bruschetta What is not clear to me: is this "elaborate" a GUI (widget, dialog,...) class?
          Never do anything related to GUI classes in another threads! All widgets have to be in the main thread.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply
          0
          • B Bruschetta

            @joeQ said in Cannot create children for a parent that is in a different thread:

            And you said

            Elaborate is on the GetterThread ??? are you true ?

            Well is obviously a misstype, i inverted the GUI Thread And the GetterThread.
            There is really no need to be rude.
            I'm here to learn and understand what i'm doing wrong.

            Yes dia is the SLOT class.

            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by
            #5

            @Bruschetta Oh, Hi friend. Sorry what i said. and welcome. Please forgive me . I come from China, so i am not good with English. I was not rude. I am sorry.

            About your question:

            My solutions:

            1. You can try this way.
            void Example::on_pushButton_clicked()
            {
                dialog *dia = new dialog();
                GetterThread *thread = new GetterThread;
            
                connect( thread,  SIGNAL(sendTwoWords( const QString &,const QString &)), dia, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection );
            
                thread->start();
                dialog->exec();
            }
            
            1. Second way.
            GetterThread::GetterThread(dialog *pDialog)//GetterThread Constructor
            {
                connect( this,  SIGNAL(sendTwoWords( const QString &,const QString &)), pDialog, SLOT(elaborate(const QString &,const QString &)), Qt::AutoConnection );
            }
            
            void Example::on_pushButton_clicked()
            {
                dialog *dia = new dialog();
                GetterThread *thread = new GetterThread(dia);
                thread->start();
                dialog->exec();
            }
            

            I am very sorry for what I said just now. best wish for you.

            Just do it!

            1 Reply Last reply
            2
            • jsulmJ jsulm

              @Bruschetta What is not clear to me: is this "elaborate" a GUI (widget, dialog,...) class?
              Never do anything related to GUI classes in another threads! All widgets have to be in the main thread.

              B Offline
              B Offline
              Bruschetta
              wrote on last edited by Bruschetta
              #6

              @jsulm
              Dia is a Widget, a widget that have to show something(the strings it's getting from the signal for example).
              Dia is created in the GUI Thread, sendTwoWords is the SIGNAL from the GetterThread;
              So the widgets is in the main thread(GUI) but i'm unable to get data from the GetterThread to show in the widget

              The comunication problem is infact between GUI Thread e GetterThread

              QObject: Cannot create children for a parent that is in a different thread.
              (Parent is GetterThread(0x2e1fbb50), parent's thread is QThread(0x202daa70), current thread is GetterThread(0x2e1fbb50)
              

              @joeQ you are wlecome, is not easy to comunicate even with our motherlanguage ;)

              I have tryied the examples you gave me but the problem is the same :( Still getting the above quoted message.
              Anyway thanx for your advices, time and hints

              jsulmJ 1 Reply Last reply
              0
              • B Bruschetta

                @jsulm
                Dia is a Widget, a widget that have to show something(the strings it's getting from the signal for example).
                Dia is created in the GUI Thread, sendTwoWords is the SIGNAL from the GetterThread;
                So the widgets is in the main thread(GUI) but i'm unable to get data from the GetterThread to show in the widget

                The comunication problem is infact between GUI Thread e GetterThread

                QObject: Cannot create children for a parent that is in a different thread.
                (Parent is GetterThread(0x2e1fbb50), parent's thread is QThread(0x202daa70), current thread is GetterThread(0x2e1fbb50)
                

                @joeQ you are wlecome, is not easy to comunicate even with our motherlanguage ;)

                I have tryied the examples you gave me but the problem is the same :( Still getting the above quoted message.
                Anyway thanx for your advices, time and hints

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Bruschetta The question is why you get "QObject: Cannot create children for a parent that is in a different thread." It is not related to the connect statement. You should check what you are doing in that thread. Do you allocate any objects with GetterThread as parent?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                B 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Bruschetta The question is why you get "QObject: Cannot create children for a parent that is in a different thread." It is not related to the connect statement. You should check what you are doing in that thread. Do you allocate any objects with GetterThread as parent?

                  B Offline
                  B Offline
                  Bruschetta
                  wrote on last edited by
                  #8

                  @jsulm You where right....
                  socket=new QTcpSocket(this); //not Working
                  socket=new QTcpSocket();//Working

                  Thanks a lot for the answer. You made my day ;)

                  jsulmJ 1 Reply Last reply
                  0
                  • B Bruschetta

                    @jsulm You where right....
                    socket=new QTcpSocket(this); //not Working
                    socket=new QTcpSocket();//Working

                    Thanks a lot for the answer. You made my day ;)

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Bruschetta Is this code located in the constructor? If so then move it to run(). Constructor is executed in the thread creating the GetterThread instance, so everything you create in constructor will be in that thread. But then you use socket in the new thread when start() is called.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    B 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @Bruschetta Is this code located in the constructor? If so then move it to run(). Constructor is executed in the thread creating the GetterThread instance, so everything you create in constructor will be in that thread. But then you use socket in the new thread when start() is called.

                      B Offline
                      B Offline
                      Bruschetta
                      wrote on last edited by
                      #10

                      @jsulm
                      Thank you for the tip but the code is already in the run()

                      jsulmJ 1 Reply Last reply
                      0
                      • B Bruschetta

                        @jsulm
                        Thank you for the tip but the code is already in the run()

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Bruschetta You're right: thread object (GetterThread) itself lives in the main thread, but not the socket :-) So the thread object cannot be parent of socket.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved