Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. why i am getting runtime error " QObject: Cannot create children for a parent that is in a different thread." ?
QtWS25 Last Chance

why i am getting runtime error " QObject: Cannot create children for a parent that is in a different thread." ?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
8 Posts 2 Posters 681 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1

    i want to start and stop interrupt at 15 second interval when receive interrupt.
    but i am getting runtime error
    " QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QApplication(0x7efcfb20), parent's thread is QThread(0xc50bf8), current thread is QThread(0x68f006c8) "

    How to resolve this error ?

    #include "MainWindow.h"
    #include "ui_MainWindow.h"
    #include <QDebug>
    #include <QTimer>
    void gpo_handler()
    {
    
        uint8_t itstatus;
    
        MainWindow m;
    
    
            // if( GPO_Activated == 1 )
            // {
            //GPO_Activated = 0;
    
               // qDebug()<<"RF write \n";
               // printf("RF write \n");
    if(m.bStatus == true)
    {
        if(m.elapsetime.elapsed()>15000)
        {
        BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFWRITE_MASK );// field is on and off
         m.bStatus =false;
        }
    }
        ST25_RETRY(BSP_NFCTAG_ReadITSTStatus_Dyn(0, &itstatus ));
    
    
    
            if( (itstatus & ST25DV_ITSTS_DYN_RFWRITE_MASK) == ST25DV_ITSTS_DYN_RFWRITE_MASK )
            {
                BSP_NFCTAG_ConfigIT(0,0);// field is on and off
                qDebug()<<"RF write \n";
    
                m.bStatus = true;
                m.elapsetime.start();
    
                //sleep(15);
               // BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFWRITE_MASK );// field is on and off
            }
    
    
    }
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        // const ST25DV_PASSWD st25dv_i2c_password = {.MsbPasswd = 0, .LsbPasswd=0};
        bStatus = false;
         /* Init of the Type Tag 5 component (ST25DV-I2C) */
          if(BSP_NFCTAG_Init(0) != NFCTAG_OK);
    
    
         /* Present configuration password */
         //  BSP_NFCTAG_PresentI2CPassword(0, st25dv_i2c_password );
    
         /* Set GPO Configuration */
    
        BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFWRITE_MASK );// field is on and off
    
       //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFWRITE_MASK );// no
       //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFGETMSG_MASK ); // No
       //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFINTERRUPT_MASK ); // No
       //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFPUTMSG_MASK );// NO
       //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFACTIVITY_MASK );// field is on
    
         while(ST25DV_GPO_Init() != NFCTAG_OK);
    
         while(ST25DV_GPO_interrupt_init(&gpo_handler) != NFCTAG_OK);
    
       //  ST25DV_GPO_DeInit();
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    jsulmJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      i want to start and stop interrupt at 15 second interval when receive interrupt.
      but i am getting runtime error
      " QObject: Cannot create children for a parent that is in a different thread.
      (Parent is QApplication(0x7efcfb20), parent's thread is QThread(0xc50bf8), current thread is QThread(0x68f006c8) "

      How to resolve this error ?

      #include "MainWindow.h"
      #include "ui_MainWindow.h"
      #include <QDebug>
      #include <QTimer>
      void gpo_handler()
      {
      
          uint8_t itstatus;
      
          MainWindow m;
      
      
              // if( GPO_Activated == 1 )
              // {
              //GPO_Activated = 0;
      
                 // qDebug()<<"RF write \n";
                 // printf("RF write \n");
      if(m.bStatus == true)
      {
          if(m.elapsetime.elapsed()>15000)
          {
          BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFWRITE_MASK );// field is on and off
           m.bStatus =false;
          }
      }
          ST25_RETRY(BSP_NFCTAG_ReadITSTStatus_Dyn(0, &itstatus ));
      
      
      
              if( (itstatus & ST25DV_ITSTS_DYN_RFWRITE_MASK) == ST25DV_ITSTS_DYN_RFWRITE_MASK )
              {
                  BSP_NFCTAG_ConfigIT(0,0);// field is on and off
                  qDebug()<<"RF write \n";
      
                  m.bStatus = true;
                  m.elapsetime.start();
      
                  //sleep(15);
                 // BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFWRITE_MASK );// field is on and off
              }
      
      
      }
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          // const ST25DV_PASSWD st25dv_i2c_password = {.MsbPasswd = 0, .LsbPasswd=0};
          bStatus = false;
           /* Init of the Type Tag 5 component (ST25DV-I2C) */
            if(BSP_NFCTAG_Init(0) != NFCTAG_OK);
      
      
           /* Present configuration password */
           //  BSP_NFCTAG_PresentI2CPassword(0, st25dv_i2c_password );
      
           /* Set GPO Configuration */
      
          BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFWRITE_MASK );// field is on and off
      
         //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFWRITE_MASK );// no
         //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFGETMSG_MASK ); // No
         //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFINTERRUPT_MASK ); // No
         //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFPUTMSG_MASK );// NO
         //  BSP_NFCTAG_ConfigIT(0,ST25DV_GPO_ENABLE_MASK | ST25DV_GPO_RFACTIVITY_MASK );// field is on
      
           while(ST25DV_GPO_Init() != NFCTAG_OK);
      
           while(ST25DV_GPO_interrupt_init(&gpo_handler) != NFCTAG_OK);
      
         //  ST25DV_GPO_DeInit();
      
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Qt-embedded-developer said in why i am getting runtime error " QObject: Cannot create children for a parent that is in a different thread." ?:

      MainWindow m;

      Why do you create an instance of MainWindow in your gpo_handler()?!

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

      Q 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Qt-embedded-developer said in why i am getting runtime error " QObject: Cannot create children for a parent that is in a different thread." ?:

        MainWindow m;

        Why do you create an instance of MainWindow in your gpo_handler()?!

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by
        #3

        @jsulm to start and stop interrupt at 15 second while receive interrupt.

        jsulmJ 1 Reply Last reply
        0
        • Q Qt embedded developer

          @jsulm to start and stop interrupt at 15 second while receive interrupt.

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

          @Qt-embedded-developer said in why i am getting runtime error " QObject: Cannot create children for a parent that is in a different thread." ?:

          to start and stop interrupt at 15 second while receive interrupt.

          You should contact the already existing MainWindow instance instead of creating a new MainWindow every time gpo_handler() is called...

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

          Q 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Qt-embedded-developer said in why i am getting runtime error " QObject: Cannot create children for a parent that is in a different thread." ?:

            to start and stop interrupt at 15 second while receive interrupt.

            You should contact the already existing MainWindow instance instead of creating a new MainWindow every time gpo_handler() is called...

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by
            #5

            @jsulm How to create MainWindow instance in MainWindow class ? Please provide example

            jsulmJ 1 Reply Last reply
            0
            • Q Qt embedded developer

              @jsulm How to create MainWindow instance in MainWindow class ? Please provide example

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

              @Qt-embedded-developer You create your MainWindow instance in main() already, right?
              I suggest to do this:

              MainWindow* mainWindowInstance{nullptr};
              
              void gpo_handler()
              {
                  uint8_t itstatus;
                  if (!mainWindowInstance) return;
                  if(mainWindowInstance.bStatus == true)
              ...
              }
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  mainWindowInstance = this;
              

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

              Q 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Qt-embedded-developer You create your MainWindow instance in main() already, right?
                I suggest to do this:

                MainWindow* mainWindowInstance{nullptr};
                
                void gpo_handler()
                {
                    uint8_t itstatus;
                    if (!mainWindowInstance) return;
                    if(mainWindowInstance.bStatus == true)
                ...
                }
                
                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    mainWindowInstance = this;
                
                Q Offline
                Q Offline
                Qt embedded developer
                wrote on last edited by
                #7

                @jsulm said in why i am getting runtime error " QObject: Cannot create children for a parent that is in a different thread." ?:

                mainWindowInstance.bStatus

                i got below error while i write above code

                error: 'nullptr' was not declared in this scope
                MainWindow* mainWindowInstance{nullptr};
                ^

                jsulmJ 1 Reply Last reply
                0
                • Q Qt embedded developer

                  @jsulm said in why i am getting runtime error " QObject: Cannot create children for a parent that is in a different thread." ?:

                  mainWindowInstance.bStatus

                  i got below error while i write above code

                  error: 'nullptr' was not declared in this scope
                  MainWindow* mainWindowInstance{nullptr};
                  ^

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

                  @Qt-embedded-developer What compiler do you use?!
                  C++11 compiler should know what nullptr is, but you also can simply use 0.

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

                  1 Reply Last reply
                  0

                  • Login

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