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 Solved General and Desktop
6 Posts 3 Posters 2.1k Views 1 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.
  • P Offline
    P Offline
    pingal
    wrote on last edited by pingal
    #1

    I've creating an object (which inherits from QObject) in my main and passing this object to another thread i.e.

    main.cpp

    QGuiApplication app(argc, argv);
    ClientListWrapper clients;
     if (!clients.initialize())
            return -1;
    args2thread->Pointer2clients = &clients;     // Passing this object to thread 
    std::thread Listening_thread(thread_function, args2thread);  // Creating a thread
    

    Now in my thread function I'm modifying client object i.e.

    myThread.cpp

    //Receive data from client here
    .
    args2thread->clients->addClient(data);  // Store the received data
    .
    

    Why QT complains about it ? And how do i solve this

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Well you create ClientListWrapper clients; in the Main thread and simply
      assigning its adress to a variable won't move it.

      Have you considered using QThread and
      https://doc.qt.io/qt-5/qobject.html#moveToThread

      1 Reply Last reply
      1
      • P Offline
        P Offline
        pingal
        wrote on last edited by
        #3

        I'm actually passing the address of clients variable to my thread. In that thread, I'm putting some data using that address. This approach works well in C++ but QT has some problem with this approach. It maybe because ClientListWrapper inherets from QObject (?)

        mrjjM 1 Reply Last reply
        0
        • P pingal

          I'm actually passing the address of clients variable to my thread. In that thread, I'm putting some data using that address. This approach works well in C++ but QT has some problem with this approach. It maybe because ClientListWrapper inherets from QObject (?)

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @pingal
          Hi
          I would guess on that too even I have not mixed std::thread with QObjects before.
          The Qt way would be to emit the data from Listening_thread via a custom signal and
          have ClientListWrapper clients connect to that.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You create a QObject derived class (and pass a parent) in the secondary thread which is not allowed. All objects of an object tree must live in the same thread. So you have two options - either move the whole object tree to the other thread or don't put the newly created QObject in the object tree from the main thread.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • P Offline
              P Offline
              pingal
              wrote on last edited by
              #6

              I've now used QThread instead of std::thread and used signal/slots to transfer data from my Listening_thread to main thread. problem solved.

              Thanks for the helpful replies.

              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