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. Signals across threads between non-child objects
Forum Updated to NodeBB v4.3 + New Features

Signals across threads between non-child objects

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 885 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.
  • J Offline
    J Offline
    John Howe
    wrote on last edited by
    #1

    Hey gang,

    I'm developing a plugin to a piece of 3rd party software, which the 3rd party software loads as a dll. In my main application, I create an instance of the 3rd party software. The dll, by necessity, runs in a separate thread. So something like this:

    mainWindow()
    {
        thirdPartyInterface();
    }
    
    plugin()
    {
        //autoloaded by thirdPartyInterface() at runtime
    }
    

    Due to threading requirements, plugin() is not a child object of either mainWindow() or thirdPartyInterface().

    I need to send a signal across from plugin() to mainWindow(), but so far have not been able to figure out how to manage this. I attempted to set the connection type as a QueuedConnection, but I think the real issue is not designating the sender correctly.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #13

      You need a pointer to the object from where the signal is coming from.How you get it is up to you and plain c++ stuff (e.g. passing a pointer around, doing the connect somewhere else where both pointers are accessible, ...).

      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
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Two object don't need to be parent/child objects to be connected to each other. The only requirement is that both have to be derived from QObject.

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

        J 1 Reply Last reply
        6
        • Christian EhrlicherC Christian Ehrlicher

          Two object don't need to be parent/child objects to be connected to each other. The only requirement is that both have to be derived from QObject.

          J Offline
          J Offline
          John Howe
          wrote on last edited by
          #3

          @Christian-Ehrlicher That's great to hear. Is there a way for me to identify the sender by the QObject name? If this is just a syntax issue, I'm relieved.

          jsulmJ 1 Reply Last reply
          0
          • J John Howe

            @Christian-Ehrlicher That's great to hear. Is there a way for me to identify the sender by the QObject name? If this is just a syntax issue, I'm relieved.

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

            @John-Howe said in Signals across threads between non-child objects:

            Is there a way for me to identify the sender by the QObject name?

            You can call sender() in the slot to get the pointer to the sender.

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

            J 1 Reply Last reply
            2
            • jsulmJ jsulm

              @John-Howe said in Signals across threads between non-child objects:

              Is there a way for me to identify the sender by the QObject name?

              You can call sender() in the slot to get the pointer to the sender.

              J Offline
              J Offline
              John Howe
              wrote on last edited by
              #5

              @jsulm Looks like that won't work.

              QObject::connect: Cannot connect (null)::sendSignal() to MainWindow::recvSignal()
              

              According to the documentation here: https://doc.qt.io/qt-5/qobject.html#sender the sender() function won't work when the signal comes from a thread different from the object's thread.

              KroMignonK 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #6

                Your sender pointer is not initialized as you can see (null).
                Passing the sender can also be done with e.g. a lambda. See the wiki

                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
                • J John Howe

                  @jsulm Looks like that won't work.

                  QObject::connect: Cannot connect (null)::sendSignal() to MainWindow::recvSignal()
                  

                  According to the documentation here: https://doc.qt.io/qt-5/qobject.html#sender the sender() function won't work when the signal comes from a thread different from the object's thread.

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

                  @John-Howe said in Signals across threads between non-child objects:

                  QObject::connect: Cannot connect (null)::sendSignal() to MainWindow::recvSignal()

                  What are do you doing to becomes this warning?

                  sender() is to be used in to slot code to be aware about which object have triggered the slot.
                  But sender() may not always be valid and his usage is not conform to Qt philosophy.
                  If you need sender, it is better to use lambda function.

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

                  J 1 Reply Last reply
                  3
                  • KroMignonK KroMignon

                    @John-Howe said in Signals across threads between non-child objects:

                    QObject::connect: Cannot connect (null)::sendSignal() to MainWindow::recvSignal()

                    What are do you doing to becomes this warning?

                    sender() is to be used in to slot code to be aware about which object have triggered the slot.
                    But sender() may not always be valid and his usage is not conform to Qt philosophy.
                    If you need sender, it is better to use lambda function.

                    J Offline
                    J Offline
                    John Howe
                    wrote on last edited by
                    #8

                    @KroMignon That warning comes from using sender() in my connect() code. I don't understand currently how I would use a lambda to find my sender.

                    KroMignonK 1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      You should not use sender() in your connect function - what should it do there?
                      sender() can be used in the slot to get the sender of the signal. But as we already told you it's not reliable and using a lambda should be preferred to due this.

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

                      J 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        You should not use sender() in your connect function - what should it do there?
                        sender() can be used in the slot to get the sender of the signal. But as we already told you it's not reliable and using a lambda should be preferred to due this.

                        J Offline
                        J Offline
                        John Howe
                        wrote on last edited by
                        #10

                        @Christian-Ehrlicher Gotcha, thanks for clearing that up. What do I call for my sender in the connect()?

                        Christian EhrlicherC 1 Reply Last reply
                        0
                        • J John Howe

                          @Christian-Ehrlicher Gotcha, thanks for clearing that up. What do I call for my sender in the connect()?

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          @John-Howe said in Signals across threads between non-child objects:

                          What do I call for my sender in the connect()?

                          I don't understand.

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

                          J 1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            @John-Howe said in Signals across threads between non-child objects:

                            What do I call for my sender in the connect()?

                            I don't understand.

                            J Offline
                            J Offline
                            John Howe
                            wrote on last edited by
                            #12

                            @Christian-Ehrlicher I have to specify a pointer to the sender in the connect(), but the sender is in another thread and out of scope to the class where my slot exists. So I'm not clear how to get the signal to the slot when the QObject sender is not a child of the QObject receiver and not in the same thread.

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              You need a pointer to the object from where the signal is coming from.How you get it is up to you and plain c++ stuff (e.g. passing a pointer around, doing the connect somewhere else where both pointers are accessible, ...).

                              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
                              1
                              • J John Howe

                                @KroMignon That warning comes from using sender() in my connect() code. I don't understand currently how I would use a lambda to find my sender.

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

                                @John-Howe said in Signals across threads between non-child objects:

                                That warning comes from using sender() in my connect() code. I don't understand currently how I would use a lambda to find my sender.

                                First, sender() is set during event loop processing before signal is called.
                                So I don't understand why you use it in connect(), this does't made sense to me :(

                                Perhaps you should begin with explaining what you want to do?

                                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

                                • Login

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