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. QObject: Cannot create children for a parent that is in a different thread, but it works, why?
Forum Updated to NodeBB v4.3 + New Features

QObject: Cannot create children for a parent that is in a different thread, but it works, why?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 5.3k 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by
    #1
    class MyThread : public QThread
    {
    public:
        virtual void run()
        {
            networkReply = networkAccessManager.get(QNetworkRequest(url));
        }
    private:
        QNetworkAccessManager networkAccessManager;
    }
    

    Runtime message:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNetworkAccessManager(0x243bb066390), parent's thread is QThread(0x243b78951b0), current thread is StockIDUpdateTask(0x243bb066380)
    

    The message is caused by QNetworkAccessManager ::get(), but the function return a valid object pointer, and when connect networkReply.finished to a slot, it works.

    What to do with the runtime message?

    jsulmJ 1 Reply Last reply
    0
    • jronaldJ jronald
      class MyThread : public QThread
      {
      public:
          virtual void run()
          {
              networkReply = networkAccessManager.get(QNetworkRequest(url));
          }
      private:
          QNetworkAccessManager networkAccessManager;
      }
      

      Runtime message:

      QObject: Cannot create children for a parent that is in a different thread.
      (Parent is QNetworkAccessManager(0x243bb066390), parent's thread is QThread(0x243b78951b0), current thread is StockIDUpdateTask(0x243bb066380)
      

      The message is caused by QNetworkAccessManager ::get(), but the function return a valid object pointer, and when connect networkReply.finished to a slot, it works.

      What to do with the runtime message?

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

      @jronald networkAccessManager is part of MyThread which itself is not the thread it manages. That means: run() is executed in the second thread but networkAccessManager is living in the thread which created MyThread instance. You should just create QNetworkAccessManager instance in run():

      class MyThread : public QThread
      {
      public:
          virtual void run()
          {
              networkAccessManager = new QNetworkAccessManager();
              networkReply = networkAccessManager->get(QNetworkRequest(url));
          }
      private:
          QNetworkAccessManager *networkAccessManager;
      }
      

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

      jronaldJ 1 Reply Last reply
      4
      • jronaldJ Offline
        jronaldJ Offline
        jronald
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • jsulmJ jsulm

          @jronald networkAccessManager is part of MyThread which itself is not the thread it manages. That means: run() is executed in the second thread but networkAccessManager is living in the thread which created MyThread instance. You should just create QNetworkAccessManager instance in run():

          class MyThread : public QThread
          {
          public:
              virtual void run()
              {
                  networkAccessManager = new QNetworkAccessManager();
                  networkReply = networkAccessManager->get(QNetworkRequest(url));
              }
          private:
              QNetworkAccessManager *networkAccessManager;
          }
          
          jronaldJ Offline
          jronaldJ Offline
          jronald
          wrote on last edited by
          #4

          @jsulm

          Yes, networkAccessManager .thread() is not MyThread, this is what confuses me, it works! Why?
          BTW, if networkAccessManager can be reused by multiple threads, it'll be appreciated.

          jsulmJ JKSHJ 2 Replies Last reply
          0
          • jronaldJ jronald

            @jsulm

            Yes, networkAccessManager .thread() is not MyThread, this is what confuses me, it works! Why?
            BTW, if networkAccessManager can be reused by multiple threads, it'll be appreciated.

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

            @jronald You should read this: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
            It can work even if the instance is in another thread, but you should make sure it is in the same thread to avoid issues.

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

            1 Reply Last reply
            0
            • jronaldJ jronald

              @jsulm

              Yes, networkAccessManager .thread() is not MyThread, this is what confuses me, it works! Why?
              BTW, if networkAccessManager can be reused by multiple threads, it'll be appreciated.

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

              @jronald said in QObject: Cannot create children for a parent that is in a different thread, but it works, why?:

              it works! Why?

              This is called "Undefined Behaviour". It doesn't follow the rules, but it seems to work. However, the problem is: it might suddenly stop working in the future.

              So, don't use it, even though it works now.

              if networkAccessManager can be reused by multiple threads, it'll be appreciated.

              QNetworkAccessManager is meant for be used from 1 thread only. I recommend that you design your software to only use it from 1 thread: When your main thread finishes downloading, emit a signal to send the data to another thread.

              Anyway, may I ask why you want to use multiple threads?

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

              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