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. Why cross-thread invoke will report “QObject: Cannot create children for a parent that is in a different thread.”?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 1.8k 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.
  • jsulmJ jsulm

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

    Crawl.WC Offline
    Crawl.WC Offline
    Crawl.W
    wrote on last edited by
    #8

    @jsulm noting.

    jsulmJ KroMignonK 2 Replies Last reply
    0
    • Crawl.WC Crawl.W

      @jsulm noting.

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

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

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

      1 Reply Last reply
      3
      • Crawl.WC Crawl.W

        @jsulm noting.

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #10

        @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.WC 1 Reply Last reply
        3
        • KroMignonK KroMignon

          @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.

          Crawl.WC Offline
          Crawl.WC Offline
          Crawl.W
          wrote on last edited by Crawl.W
          #11

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

          J.HilkJ 1 Reply Last reply
          1
          • Crawl.WC Crawl.W

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

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #12

            @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


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

            Crawl.WC 2 Replies Last reply
            5
            • J.HilkJ J.Hilk

              @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

              Crawl.WC Offline
              Crawl.WC Offline
              Crawl.W
              wrote on last edited by Crawl.W
              #13

              @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
              1
              • Crawl.WC Crawl.W

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

                K Offline
                K Offline
                kuzulis
                Qt Champions 2020
                wrote on last edited by
                #14

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

                Crawl.WC 1 Reply Last reply
                6
                • K kuzulis

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

                  Crawl.WC Offline
                  Crawl.WC Offline
                  Crawl.W
                  wrote on last edited by Crawl.W
                  #15

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

                  1 Reply Last reply
                  1
                  • J.HilkJ J.Hilk

                    @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

                    Crawl.WC Offline
                    Crawl.WC Offline
                    Crawl.W
                    wrote on last edited by
                    #16

                    @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?

                    jsulmJ KroMignonK 2 Replies Last reply
                    0
                    • Crawl.WC Crawl.W

                      @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?

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

                      @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.WC 1 Reply Last reply
                      5
                      • Crawl.WC Crawl.W

                        @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?

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #18

                        @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
                        5
                        • jsulmJ jsulm

                          @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.

                          Crawl.WC Offline
                          Crawl.WC Offline
                          Crawl.W
                          wrote on last edited by
                          #19

                          @jsulm Great,I got it!Thanks.

                          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