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. Error: QObject::setParent: Cannot set parent, new parent is in a different thread
Forum Updated to NodeBB v4.3 + New Features

Error: QObject::setParent: Cannot set parent, new parent is in a different thread

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 973 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.
  • S Offline
    S Offline
    shreya_agrawal
    wrote on last edited by
    #1

    I have a Left class and a CleaningThread class, which is a private member of the Left class. Now, I have a cleanValvesPerPage function in my Left class. I am calling this function by overriding the run method of CleaningThread class. This thread starts when a certain Clean button is clicked.
    Now, after cleaning the valves on the current page, the onNextButtonClicked() slot is called. But, this slot is present in the Left class. Due to this, I am getting this error: QObject::setParent: Cannot set parent, new parent is in a different thread. So, the program ends unexpectedly at this point.
    If someone has faced the same issue and could guide me towards an alternate approach, it would be very helpful.

    This is my CleaningThread class implementation:

    private:
        class CleaningThread : public QThread
        {
            public:
                CleaningThread(Left *parent);
                void startCleaning();
                void stopCleaning();
    
            protected:
                void run() override;
    
            private:
                Left *parentLeft;
                std::atomic<bool> stop;
        };
    

    This is my onCleanButtonClicked() function:

    void Left::onCleanButtonClicked()
    {
        cleaningThread = new CleaningThread(this);
        cleaningThread->startCleaning();
        connect(cleaningThread, SIGNAL(finished()), this, SLOT(onCleaningFinished()));
        cleaningThread->start();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What are you doing in that class ?
      It looks like you are trying to reparent stuff when you should not.

      Based on the names, you might even be trying to access GUI elements from a different thread which is forbidden.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        What are you doing in that class ?
        It looks like you are trying to reparent stuff when you should not.

        Based on the names, you might even be trying to access GUI elements from a different thread which is forbidden.

        S Offline
        S Offline
        shreya_agrawal
        wrote on last edited by
        #3

        @SGaist
        Thank you for your reply!

        I am trying to run a particular function cleanValvesOnPage through the CleaningThread class.

        This is how I am overriding the run() function of the CleaningThread class:

        void Left::CleaningThread::run()
        {
            int startIndex = parentLeft->currentIndex;
            parentLeft->cleanValvesOnPage(startIndex);
            parentLeft->cleaningThread = nullptr;
        }
        

        If I can't access GUI elements through a different thread then is there any alternative approach to do it without having the risk of freezing the ui?

        Pl45m4P 1 Reply Last reply
        0
        • S shreya_agrawal

          @SGaist
          Thank you for your reply!

          I am trying to run a particular function cleanValvesOnPage through the CleaningThread class.

          This is how I am overriding the run() function of the CleaningThread class:

          void Left::CleaningThread::run()
          {
              int startIndex = parentLeft->currentIndex;
              parentLeft->cleanValvesOnPage(startIndex);
              parentLeft->cleaningThread = nullptr;
          }
          

          If I can't access GUI elements through a different thread then is there any alternative approach to do it without having the risk of freezing the ui?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @shreya_agrawal

          Send a signal to a slot and execute the function there?!


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          S 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @shreya_agrawal

            Send a signal to a slot and execute the function there?!

            S Offline
            S Offline
            shreya_agrawal
            wrote on last edited by
            #5

            @Pl45m4
            Actually, signals and slots are not supported in nested classes in Qt.

            Pl45m4P 1 Reply Last reply
            0
            • S shreya_agrawal

              @Pl45m4
              Actually, signals and slots are not supported in nested classes in Qt.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @shreya_agrawal

              What "nested" classes? Your use case is the perfect example of forcing the use of any thread related stuff when it's not needed at all...
              What should your cleaningThread do? Modify the GUI? Direct or access via signals never blocks unless you add some blocking loops.
              So I think you should re-design your app structure


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              S 1 Reply Last reply
              2
              • Pl45m4P Pl45m4

                @shreya_agrawal

                What "nested" classes? Your use case is the perfect example of forcing the use of any thread related stuff when it's not needed at all...
                What should your cleaningThread do? Modify the GUI? Direct or access via signals never blocks unless you add some blocking loops.
                So I think you should re-design your app structure

                S Offline
                S Offline
                shreya_agrawal
                wrote on last edited by
                #7

                @Pl45m4
                Okay, I got your point. Basically, I was trying to update the GUI by using the cleaningThread. But now, I am just passing a signal in the run() method of the cleaningThread. This signal connects to the appropriate slot and does the cleaning action. Please let me know if this is the correct approach or I am still missing on something?

                SGaistS Pl45m4P 2 Replies Last reply
                0
                • S shreya_agrawal

                  @Pl45m4
                  Okay, I got your point. Basically, I was trying to update the GUI by using the cleaningThread. But now, I am just passing a signal in the run() method of the cleaningThread. This signal connects to the appropriate slot and does the cleaning action. Please let me know if this is the correct approach or I am still missing on something?

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @shreya_agrawal is this the real code of your application ? Because using a thread to just trigger a GUI update is more than overkill.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  S 1 Reply Last reply
                  1
                  • S shreya_agrawal

                    @Pl45m4
                    Okay, I got your point. Basically, I was trying to update the GUI by using the cleaningThread. But now, I am just passing a signal in the run() method of the cleaningThread. This signal connects to the appropriate slot and does the cleaning action. Please let me know if this is the correct approach or I am still missing on something?

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #9

                    @shreya_agrawal said in Error: QObject::setParent: Cannot set parent, new parent is in a different thread:

                    Please let me know if this is the correct approach or I am still missing on something?

                    I woudn't call it "correct"...
                    Like @SGaist said, why do you think you need a thread? To emit a signal?!


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @shreya_agrawal is this the real code of your application ? Because using a thread to just trigger a GUI update is more than overkill.

                      S Offline
                      S Offline
                      shreya_agrawal
                      wrote on last edited by
                      #10

                      @SGaist
                      Thank you for your response !
                      I realized that using threads won't be necessary in my use case, since I majorly have to update the GUI, which is recommended to be done by the main thread itself. So, now I am directly calling the cleaning function. Thank you for your time though.

                      1 Reply Last reply
                      0
                      • S shreya_agrawal has marked this topic as solved on

                      • Login

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