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. About the right way to use QThread:error:Cannot create children for a parent that is in a different thread.

About the right way to use QThread:error:Cannot create children for a parent that is in a different thread.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 4.0k 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.
  • M Offline
    M Offline
    MartinChan
    wrote on 21 Jun 2016, 13:49 last edited by
    #1

    I am trying to write a program about using two approach switches to control two cameras to work.I tried to use a multi-thread struct:the first thread is the ui. class(main thread),the second is the modbus reading thread which can read in the information and the third thread is to control which camera to work.

    SO when I tried the singal-slots way to do the work like this:

    qthreads::qthreads(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::qthreads)
    {
        ui->setupUi(this);
        modclient *h=new modclient;
        ccd2 *ccd=new ccd2;
        h->moveToThread(&THREAD_Get_Modbus);
        ccd->moveToThread(&THREAD_Get_Frame);
        connect(&THREAD_Get_Modbus,&QThread::finished,h,&QObject::deleteLater);
        connect(&THREAD_Get_Frame,&QThread::finished,ccd,&QObject::deleteLater);
    
        connect(ui->pushButton,SIGNAL(clicked(bool)),h,SLOT(connecttoModbusServer()));
        connect(h,SIGNAL(finishedreading(CAM)),ccd,SLOT(getframe(CAM)));
        THREAD_Get_Frame.start();
        THREAD_Get_Modbus.start();
    
    }
    

    When i start my program,it always reminds me of "QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QModbusTcpClient(0x1498298), parent's thread is QThread(0x1452c30), current thread is QThread(0x12ff88c)".

    So,what's wrong with my code?Is there possible to use three or more threads in Qt?

    R 1 Reply Last reply 21 Jun 2016, 15:10
    0
    • M MartinChan
      21 Jun 2016, 13:49

      I am trying to write a program about using two approach switches to control two cameras to work.I tried to use a multi-thread struct:the first thread is the ui. class(main thread),the second is the modbus reading thread which can read in the information and the third thread is to control which camera to work.

      SO when I tried the singal-slots way to do the work like this:

      qthreads::qthreads(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::qthreads)
      {
          ui->setupUi(this);
          modclient *h=new modclient;
          ccd2 *ccd=new ccd2;
          h->moveToThread(&THREAD_Get_Modbus);
          ccd->moveToThread(&THREAD_Get_Frame);
          connect(&THREAD_Get_Modbus,&QThread::finished,h,&QObject::deleteLater);
          connect(&THREAD_Get_Frame,&QThread::finished,ccd,&QObject::deleteLater);
      
          connect(ui->pushButton,SIGNAL(clicked(bool)),h,SLOT(connecttoModbusServer()));
          connect(h,SIGNAL(finishedreading(CAM)),ccd,SLOT(getframe(CAM)));
          THREAD_Get_Frame.start();
          THREAD_Get_Modbus.start();
      
      }
      

      When i start my program,it always reminds me of "QObject: Cannot create children for a parent that is in a different thread.
      (Parent is QModbusTcpClient(0x1498298), parent's thread is QThread(0x1452c30), current thread is QThread(0x12ff88c)".

      So,what's wrong with my code?Is there possible to use three or more threads in Qt?

      R Offline
      R Offline
      rafael
      wrote on 21 Jun 2016, 15:10 last edited by
      #2

      @MartinChan

      try to copy the example from the link below.

      https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

      don't include a while loop inside your thread as it will block and your application will be unresponsive use a timer if you want to poll data from other threads.

      M 1 Reply Last reply 21 Jun 2016, 15:21
      0
      • R rafael
        21 Jun 2016, 15:10

        @MartinChan

        try to copy the example from the link below.

        https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

        don't include a while loop inside your thread as it will block and your application will be unresponsive use a timer if you want to poll data from other threads.

        M Offline
        M Offline
        MartinChan
        wrote on 21 Jun 2016, 15:21 last edited by
        #3

        @rafael thx first and you said don't use a while loop in my thread,however the thread I created is used to scan the input so I have to use a 'infinite long' loop to read back the input. So I must change while loop to another kind of loop?

        K 1 Reply Last reply 21 Jun 2016, 15:36
        0
        • M MartinChan
          21 Jun 2016, 15:21

          @rafael thx first and you said don't use a while loop in my thread,however the thread I created is used to scan the input so I have to use a 'infinite long' loop to read back the input. So I must change while loop to another kind of loop?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 21 Jun 2016, 15:36 last edited by
          #4

          @MartinChan
          Hi,

          So,what's wrong with my code?

          Most probably, you have children objects created in the constructor of your worker object. So Qt's complaining when you invoke QObject::moveToThread(). You should fix that - child QObject instances and their parent can't have different thread affinity.

          Is there possible to use three or more threads in Qt?

          As many as you wish (and as many the OS will allow). The problem here is not with the threads, but how you use them.

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0

          1/4

          21 Jun 2016, 13:49

          • Login

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