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. Passing QObjects across threads via signals and slots

Passing QObjects across threads via signals and slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 1.8k Views 1 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.
  • JKSHJ JKSH

    @Mozzie said in can I use MyClass& in Qt signal and slots?:

    So, if I use const MyClass& as paremeter, I can use it between threads?

    Yes (assuming that your class is allowed to be copied between threads).

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JKSH
    #1

    [Forked from https://forum.qt.io/topic/125838/can-i-use-myclass-in-qt-signal-and-slots/ --JKSH]

    @JKSH
    I don't use threads, nor need to emit signals passing my own classes, so this is purely a for-my-information.

    So even for const MyClass& you are saying Qt copies the instance? Which would rule out anything QObject-derived. There isn't a way to just pass a const reference to the original across threads?

    jsulmJ KroMignonK 2 Replies Last reply
    0
    • JonBJ JonB

      [Forked from https://forum.qt.io/topic/125838/can-i-use-myclass-in-qt-signal-and-slots/ --JKSH]

      @JKSH
      I don't use threads, nor need to emit signals passing my own classes, so this is purely a for-my-information.

      So even for const MyClass& you are saying Qt copies the instance? Which would rule out anything QObject-derived. There isn't a way to just pass a const reference to the original across threads?

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

      @JonB said in can I use MyClass& in Qt signal and slots?:

      Qt copies the instance

      For queued connections (default for across threads) it does

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

      Christian EhrlicherC 1 Reply Last reply
      2
      • jsulmJ jsulm

        @JonB said in can I use MyClass& in Qt signal and slots?:

        Qt copies the instance

        For queued connections (default for across threads) it does

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #3

        @jsulm said in can I use MyClass& in Qt signal and slots?:

        it does

        There is no other chance than copying it :)

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • JonBJ JonB

          [Forked from https://forum.qt.io/topic/125838/can-i-use-myclass-in-qt-signal-and-slots/ --JKSH]

          @JKSH
          I don't use threads, nor need to emit signals passing my own classes, so this is purely a for-my-information.

          So even for const MyClass& you are saying Qt copies the instance? Which would rule out anything QObject-derived. There isn't a way to just pass a const reference to the original across threads?

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

          @JonB said in can I use MyClass& in Qt signal and slots?:

          So even for const MyClass& you are saying Qt copies the instance? Which would rule out anything QObject-derived. There isn't a way to just pass a const reference to the original across threads?

          With Qt, @JonB means QMetaObject::activate(), which is called when emitting a signal.
          Because source and destination are in different thread, this method is kind enough to create a copy of the current instance and pass the copy to the called slot/functor.

          Of course the QMetaObject needs to be aware about how to create a copy, so you have to register the type with qRegisterMetaType(). And the class needs to implement a copy constructor.

          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
          2
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @jsulm , @Christian-Ehrlicher , @KroMignon
            Hmm, interesting, thank you. So for example no const QObject & is possible.

            Maybe I'm thinking too much about references, &. I can instead pass a pointer, const QObject *, right? Provided I am careful about lifetime and read-writes, that is OK? (And I don't need qRegisterMetaType() for that?)

            Christian EhrlicherC KroMignonK mrjjM 3 Replies Last reply
            0
            • JonBJ JonB

              @jsulm , @Christian-Ehrlicher , @KroMignon
              Hmm, interesting, thank you. So for example no const QObject & is possible.

              Maybe I'm thinking too much about references, &. I can instead pass a pointer, const QObject *, right? Provided I am careful about lifetime and read-writes, that is OK? (And I don't need qRegisterMetaType() for that?)

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JonB said in can I use MyClass& in Qt signal and slots?:

              Provided I am careful about lifetime and read-writes, that is OK?

              Yes, but very error-prone.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • JonBJ JonB

                @jsulm , @Christian-Ehrlicher , @KroMignon
                Hmm, interesting, thank you. So for example no const QObject & is possible.

                Maybe I'm thinking too much about references, &. I can instead pass a pointer, const QObject *, right? Provided I am careful about lifetime and read-writes, that is OK? (And I don't need qRegisterMetaType() for that?)

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

                @JonB said in can I use MyClass& in Qt signal and slots?:

                Hmm, interesting, thank you. So for example no const QObject & is possible.

                There are more than one level to take in account:

                • the signature of signal
                • the signature of slot
                • the QMetaObject::activate() call

                Using const QObject & for signal does only means that when calling the signal function, no copy will be done.
                But as sender and receiver are in different threads, QMetaObject::activate() will create a copy of parameter before add slot call in receiver thread QEventLoop.

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

                JonBJ 1 Reply Last reply
                2
                • JonBJ JonB

                  @jsulm , @Christian-Ehrlicher , @KroMignon
                  Hmm, interesting, thank you. So for example no const QObject & is possible.

                  Maybe I'm thinking too much about references, &. I can instead pass a pointer, const QObject *, right? Provided I am careful about lifetime and read-writes, that is OK? (And I don't need qRegisterMetaType() for that?)

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @JonB
                  Hi
                  Sending raw pointers between threads is as recommended
                  as using a torch to investigate why all the electrical powers went out
                  in the Gasoline factory. 💥😜

                  1 Reply Last reply
                  4
                  • KroMignonK KroMignon

                    @JonB said in can I use MyClass& in Qt signal and slots?:

                    Hmm, interesting, thank you. So for example no const QObject & is possible.

                    There are more than one level to take in account:

                    • the signature of signal
                    • the signature of slot
                    • the QMetaObject::activate() call

                    Using const QObject & for signal does only means that when calling the signal function, no copy will be done.
                    But as sender and receiver are in different threads, QMetaObject::activate() will create a copy of parameter before add slot call in receiver thread QEventLoop.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @KroMignon said in can I use MyClass& in Qt signal and slots?:

                    Using const QObject & for signal does only means that when calling the signal function, no copy will be done.
                    But as sender and receiver are in different threads, QMetaObject::activate() will create a copy of parameter before add slot call in receiver thread QEventLoop.

                    But since QObject is not copyable I don't see how this case would be allowed?

                    Since you are all saying that neither & nor * are good for QObject across threads, does this mean in practice that you cannot pass a QObject from a signal to a slot?

                    I am getting very lost now. Because when people ask "how do I pass the widget which caused a signal to the slot if that's what I need to do" one of the possible answers we give is "write a lambda which accepts the widget as an extra parameter", like:

                    connect(checkbox, &QCheckBox::pressed, that, [checkbox]() { qDebug() << checkbox; });
                    

                    Maybe this doesn't count as a parameter. But from Python this would be:

                    checkbox.pressed.connect(lambda checkbox=checkbox: print(checkbox))
                    

                    There's no difference in Python lambdas between what goes in the [...] versus (...) from the C++ lambda.

                    My head hurts.... :)

                    KroMignonK JKSHJ 2 Replies Last reply
                    0
                    • JonBJ JonB

                      @KroMignon said in can I use MyClass& in Qt signal and slots?:

                      Using const QObject & for signal does only means that when calling the signal function, no copy will be done.
                      But as sender and receiver are in different threads, QMetaObject::activate() will create a copy of parameter before add slot call in receiver thread QEventLoop.

                      But since QObject is not copyable I don't see how this case would be allowed?

                      Since you are all saying that neither & nor * are good for QObject across threads, does this mean in practice that you cannot pass a QObject from a signal to a slot?

                      I am getting very lost now. Because when people ask "how do I pass the widget which caused a signal to the slot if that's what I need to do" one of the possible answers we give is "write a lambda which accepts the widget as an extra parameter", like:

                      connect(checkbox, &QCheckBox::pressed, that, [checkbox]() { qDebug() << checkbox; });
                      

                      Maybe this doesn't count as a parameter. But from Python this would be:

                      checkbox.pressed.connect(lambda checkbox=checkbox: print(checkbox))
                      

                      There's no difference in Python lambdas between what goes in the [...] versus (...) from the C++ lambda.

                      My head hurts.... :)

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

                      @JonB said in can I use MyClass& in Qt signal and slots?:

                      But since QObject is not copyable I don't see how this case would be allowed?

                      This case is not possible/allowed ;) That's the point.
                      when sender and receiver are in different threads, all parameter types needs to have a copy constructor and have to be registered with qRegisterMetaType().

                      QObject bassed type cannot be used for cross-threaded or queued connection.

                      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
                      0
                      • JonBJ JonB

                        @KroMignon said in can I use MyClass& in Qt signal and slots?:

                        Using const QObject & for signal does only means that when calling the signal function, no copy will be done.
                        But as sender and receiver are in different threads, QMetaObject::activate() will create a copy of parameter before add slot call in receiver thread QEventLoop.

                        But since QObject is not copyable I don't see how this case would be allowed?

                        Since you are all saying that neither & nor * are good for QObject across threads, does this mean in practice that you cannot pass a QObject from a signal to a slot?

                        I am getting very lost now. Because when people ask "how do I pass the widget which caused a signal to the slot if that's what I need to do" one of the possible answers we give is "write a lambda which accepts the widget as an extra parameter", like:

                        connect(checkbox, &QCheckBox::pressed, that, [checkbox]() { qDebug() << checkbox; });
                        

                        Maybe this doesn't count as a parameter. But from Python this would be:

                        checkbox.pressed.connect(lambda checkbox=checkbox: print(checkbox))
                        

                        There's no difference in Python lambdas between what goes in the [...] versus (...) from the C++ lambda.

                        My head hurts.... :)

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #11

                        @JonB said in Passing QObjects across threads via signals and slots:

                        does this mean in practice that you cannot pass a QObject from a signal to a slot?

                        Yes, you can. QNetworkAccessManager::finished(QNetworkReply*) is one such signal; QNetworkReply is a QObject.

                        Since you are all saying that neither & nor * are good for QObject across threads

                        Hold up. Take a deep breath. Let's untangle things a bit.

                        • You cannot use const QObject& as a signal/slot argument between threads, ever. Because QObjects are not copyable.
                        • You can use QObject* as a signal/slot argument between threads. @Christian-Ehrlicher and @mrjj did not say it's not allowed; they said it's dangerous.

                        I am getting very lost now. Because when people ask "how do I pass the widget which caused a signal to the slot if that's what I need to do" one of the possible answers we give is "write a lambda which accepts the widget as an extra parameter", like:

                        connect(checkbox, &QCheckBox::pressed, that, [checkbox]() { qDebug() << checkbox; });
                        

                        Well, there's no multithreading here. So it's not dangerous.

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        3

                        • Login

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