Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Why cross-thread invoke will report “QObject: Cannot create children for a parent that is in a different thread.”?

    General and Desktop
    5
    19
    1007
    Loading More Posts
    • 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.
    • Crawl.W
      Crawl.W last edited by Crawl.W

      Such as the title,confusing warning!Such as a member funcation of QSerialPort's instance invoked by child thread. I do not wanna get a solution,rahter than know why for the warning is given.

      //main.cpp
      int main(int argc, char *argv[])
      {
          //...
          TestClass *testclass2 = TestClass::GetInstance();
      
          TempMyThread *mythread = new TempMyThread;
          mythread->start();
          //...
      }
      
      //tempthread.cpp
      void TempMyThread::run(){
          TestClass *testclass2 = TestClass::GetInstance();
          qDebug() << "temp thread " << testclass2;
      
          testclass2->myserialport->write("1");
      }
      

      myserialport is TestClass's member var.

      J.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @Crawl.W last edited by

        hi @Crawl.W

        it's not a warning, its an error and not really confusing.

        What is the issue exactly ?

        Chances are, the the function is executed in the thread of the function caller. If you inside the function create a new QObject with for example this as parent, then you're trying to parent a Object to a parent that lives in a different thread compared to the creating thread.

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        Crawl.W 1 Reply Last reply Reply Quote 3
        • Crawl.W
          Crawl.W last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • Crawl.W
            Crawl.W @J.Hilk last edited by Crawl.W

            @J.Hilk But If I just call a funcation like QSerialPort::write(), there is no qobject created in the whole process.That's your a assumption, and sometimes it's not tenable.

            J.Hilk 1 Reply Last reply Reply Quote 0
            • J.Hilk
              J.Hilk Moderators @Crawl.W last edited by

              @Crawl-W
              of course, that's my assumption, you haven't shown any code. What else can I do but assume ?

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              Crawl.W 1 Reply Last reply Reply Quote 2
              • Crawl.W
                Crawl.W @J.Hilk last edited by

                @J.Hilk About QSerialPort::write() or QSerialPort::open()? I had tried to seach the source, but no object created seemingly.

                jsulm 1 Reply Last reply Reply Quote 0
                • jsulm
                  jsulm Lifetime Qt Champion @Crawl.W last edited by

                  @Crawl.W What else do you do in that other thread?

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

                  Crawl.W 1 Reply Last reply Reply Quote 0
                  • Crawl.W
                    Crawl.W @jsulm last edited by

                    @jsulm noting.

                    jsulm KroMignon 2 Replies Last reply Reply Quote 0
                    • jsulm
                      jsulm Lifetime Qt Champion @Crawl.W last edited by

                      @Crawl.W Well, without source code others can only guess...

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

                      1 Reply Last reply Reply Quote 3
                      • KroMignon
                        KroMignon @Crawl.W last edited by

                        @Crawl.W said in Why cross-thread invoke will report “QObject: Cannot create children for a parent that is in a different thread.”?:

                        noting.

                        You are doing something or you would not have this error message.
                        If you don't want to show your code, then you will have to find yourself where you create a QObject instance with a parent in a different thread.

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        Crawl.W 1 Reply Last reply Reply Quote 3
                        • Crawl.W
                          Crawl.W @KroMignon last edited by Crawl.W

                          I had assumed that the question only need simple description.Code has supplymented.

                          J.Hilk 1 Reply Last reply Reply Quote 1
                          • J.Hilk
                            J.Hilk Moderators @Crawl.W last edited by

                            @Crawl.W

                            now it's obvious.

                            Your singleton is created in the main thread. you write to it inside the worker thread. during write process, a QTimer is created and set, to monitor timeouts. That is created from the wrong thread of course. -> Therefore the error

                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                            Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            Crawl.W 2 Replies Last reply Reply Quote 5
                            • Crawl.W
                              Crawl.W @J.Hilk last edited by Crawl.W

                              @J.Hilk So it is,You rocks!But I did not find where QTimer created, can you post those source?

                              K 1 Reply Last reply Reply Quote 1
                              • K
                                kuzulis Qt Champions 2020 @Crawl.W last edited by

                                https://github.com/qt/qtserialport/blob/5.12/src/serialport/qserialport_win.cpp#L625

                                Crawl.W 1 Reply Last reply Reply Quote 6
                                • Crawl.W
                                  Crawl.W @kuzulis last edited by Crawl.W

                                  @kuzulis @J-Hilk Thanks for everyone,I saw.

                                  1 Reply Last reply Reply Quote 1
                                  • Crawl.W
                                    Crawl.W @J.Hilk last edited by

                                    @J.Hilk As show in source,the QTimer is very strange, which is single shot and no interval set.Why?Why do not call his timeout's slot function directly?

                                    jsulm KroMignon 2 Replies Last reply Reply Quote 0
                                    • jsulm
                                      jsulm Lifetime Qt Champion @Crawl.W last edited by

                                      @Crawl.W said in Why cross-thread invoke will report “QObject: Cannot create children for a parent that is in a different thread.”?:

                                      Why do not call his timeout's slot function directly?

                                      Because then it would be a synchronous call - write() would block until it's finished. But since it is an asynchronous API it should not block the caller.

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

                                      Crawl.W 1 Reply Last reply Reply Quote 5
                                      • KroMignon
                                        KroMignon @Crawl.W last edited by

                                        @Crawl.W said in Why cross-thread invoke will report “QObject: Cannot create children for a parent that is in a different thread.”?:

                                        Why do not call his timeout's slot function directly?

                                        This is done to call the timeout function after all events in thread queue have been processed.
                                        Here the extract for QTimer::interval:

                                        The default value for this property is 0. A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed

                                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                        1 Reply Last reply Reply Quote 5
                                        • Crawl.W
                                          Crawl.W @jsulm last edited by

                                          @jsulm Great,I got it!Thanks.

                                          1 Reply Last reply Reply Quote 0
                                          • First post
                                            Last post