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.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 6.3k Views 2 Watching
  • 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.
  • A Offline
    A Offline
    Alexanov
    wrote on last edited by Alexanov
    #1

    Hi
    I have two thread :1- Main thread GUi 2-server_Thread(class of QThread)
    in server_Thread I create udpsocket like this :

    //server_Thread.h
     private :
    QUdpSocket *udpSocket;
    
    //server_Thread.c
    void thread_server::run(){
    .
    .
        udpSocket = new QUdpSocket(this);
    }
    

    but when thread_server.c start and go to run function. I see this error: "QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QUdpSocket(0x691e40), parent's thread is QThread(0x610090), current thread is thread_server(0x7fffffffe300)"

    JKSHJ jsulmJ 2 Replies Last reply
    0
    • A Alexanov

      Hi
      I have two thread :1- Main thread GUi 2-server_Thread(class of QThread)
      in server_Thread I create udpsocket like this :

      //server_Thread.h
       private :
      QUdpSocket *udpSocket;
      
      //server_Thread.c
      void thread_server::run(){
      .
      .
          udpSocket = new QUdpSocket(this);
      }
      

      but when thread_server.c start and go to run function. I see this error: "QObject: Cannot create children for a parent that is in a different thread.
      (Parent is QUdpSocket(0x691e40), parent's thread is QThread(0x610090), current thread is thread_server(0x7fffffffe300)"

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

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

      //server_Thread.c
      void thread_server::run(){
      .
      .
          udpSocket = new QUdpSocket(this);
      }
      

      thread_server is a QObject that lives in the main thread. Therefore, you cannot make it the parent for any QObjects that you create in your thread.

      Why do you pass this to the QUdpSocket constructor?

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      4
      • S Offline
        S Offline
        stryga42
        wrote on last edited by
        #3

        Hi Alexanov! As JKSH points out, all QObject instances (and instances from derived classes of course) in a "family" (that is with parent / child relation) have to live in the same thread. Beside other things this is very important for signal / slots and everything going through the event queue. You can push instances between threads, as long as they have no parent, see void QObject::moveToThread(QThread *targetThread). Make sure you have a clear understanding which family belongs to which thread.
        Be aware that signal / slot mechanism works differently depending on the thread-affinity of sender and receiver. As starting point you could check enum Qt::ConnectionType

        1 Reply Last reply
        3
        • A Alexanov

          Hi
          I have two thread :1- Main thread GUi 2-server_Thread(class of QThread)
          in server_Thread I create udpsocket like this :

          //server_Thread.h
           private :
          QUdpSocket *udpSocket;
          
          //server_Thread.c
          void thread_server::run(){
          .
          .
              udpSocket = new QUdpSocket(this);
          }
          

          but when thread_server.c start and go to run function. I see this error: "QObject: Cannot create children for a parent that is in a different thread.
          (Parent is QUdpSocket(0x691e40), parent's thread is QThread(0x610090), current thread is thread_server(0x7fffffffe300)"

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

          @Alexanov To add to @JKSH : you pass this to QUdpSocket constructor. run() is executed in the second thread but thread_server instance (to which this points) lives in the thread where it was created. Just do not pass this to QUdpSocket constructor.

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

          A 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Alexanov To add to @JKSH : you pass this to QUdpSocket constructor. run() is executed in the second thread but thread_server instance (to which this points) lives in the thread where it was created. Just do not pass this to QUdpSocket constructor.

            A Offline
            A Offline
            Alexanov
            wrote on last edited by
            #5

            @jsulm
            did you say delete "this" from my object like below? (object without any parent)

            udpSocket = new QUdpSocket();
            
            jsulmJ VRoninV 2 Replies Last reply
            0
            • A Alexanov

              @jsulm
              did you say delete "this" from my object like below? (object without any parent)

              udpSocket = new QUdpSocket();
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Alexanov yes

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

              1 Reply Last reply
              1
              • A Alexanov

                @jsulm
                did you say delete "this" from my object like below? (object without any parent)

                udpSocket = new QUdpSocket();
                
                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                @Alexanov You still have to remember to delete it though. There are 3 ways:

                • connect some signal (QThread::finished?) to the deleteLater slot
                • delete it manually in QThread's destructor
                • use smart pointers.

                I'd go for the 3rd, change QUdpSocket *udpSocket; to std::unique_ptr<QUdpSocket> udpSocket;

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2

                • Login

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