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. Connect one object to many objects of same type.
QtWS25 Last Chance

Connect one object to many objects of same type.

Scheduled Pinned Locked Moved General and Desktop
multipleobjectsconnect
8 Posts 3 Posters 3.7k 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.
  • C Offline
    C Offline
    code_fodder
    wrote on 12 Oct 2015, 13:38 last edited by
    #1

    So I have the following setup:

                   /--- Device[1]
    Controller ---+---- Device[2]
                   \--- Device[3]
    

    Each object is in its own thread.

    to controller this is relatively easy, I just connect all the device signals to a slot in the controller and pass the device index as a parameter so the controller can identify which device.

    However, I am unsure how is best to do the same in the other direction (controller to device). The best I have come up with is a similar scheme, where I connect a signal in controller to a slot in each of the devices. I can still pass an index so if the messages is directed at Device[1], then Device[2] and Device[3] can ignore it. However, is this a huge overhead in terms of data repetition? - i.e. is the data sent three times?

    Is there a better way?

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yeckel
      wrote on 12 Oct 2015, 15:57 last edited by
      #2

      Hi indeed you can send the index, or you can use QObject::sender() method which is returning pointer to emitting QObject to identify who sent the message, or you can use QSignalMapper to add index.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        code_fodder
        wrote on 12 Oct 2015, 16:01 last edited by
        #3

        Hi Thanks for that.

        In the case where controller needs to send something to a specific device, how can that be done?

        If I connect "send" signal in controller, to each of the slots then I may get the data sent three times... which is not efficient.

        I could connect separate signals to each device, but I have to then create the signals dynamically because I only know how many devices I will have at run time... not sure if that is possible....?

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yeckel
          wrote on 12 Oct 2015, 16:10 last edited by
          #4

          Well then just call the method at the object directly, QMetaObject::invokeMethod(..., Qt::QueuedConnection), to make it thread save.

          C 1 Reply Last reply 12 Oct 2015, 16:28
          0
          • Y yeckel
            12 Oct 2015, 16:10

            Well then just call the method at the object directly, QMetaObject::invokeMethod(..., Qt::QueuedConnection), to make it thread save.

            C Offline
            C Offline
            code_fodder
            wrote on 12 Oct 2015, 16:28 last edited by code_fodder 10 Dec 2015, 16:58
            #5

            @yeckel
            Do you mean in the controller, keep a list of device pointer that are in different threads and then do something like:

            edit - corrected the syntax
            QMetaObject::invokeMethod(devices[x], "handleMessage", Qt::QueuedConnection, Q_ARG(QByteArray msgData)));

            Is that right? - is that not breaking the whole signal/slots methodology?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              code_fodder
              wrote on 13 Oct 2015, 08:00 last edited by
              #6

              So invoking slots directly works, but it breaks the whole methodology of slots/signals, because we are invoking a slot in a foreign object without a signal or connection... effectively punching a hole through the wall and grabbing what we want - so it also breaks the encapsulation principals too (not the mention thread boundary).

              So finally I decided I have to dream something up else...
              Because I only know how many devices I will have at run time, creating signals dynamically as each device registers is appealing and possible, but its not very easy to do and not maintainable.

              However we can instantiate objects dynamically, where the object contains a signal that can be used to map one-to-one to a device. Effectively creating a "postbox" object where its signal can be connected to the equivalent device at run-time. The setup is shown below:

              |-------------------------|
              |                         |
              |             Postbox[1]--+------Device[1]
              | Controller  Postbox[2]--+------Device[2]
              |             Postbox[3]--+------Device[3]
              |                         |   :
              |             Postbox[x]--+------Device[x]
              |                         |
              |-------------------------|
              

              Where the controller object contains an array (or vector) of postbox objects. For each device that registers with the controller it creates a new instance of postbox, connects its signal to the devices slot and then adds the postbox to the array. Then to post a message to, for example device[3], the controller simply calls a function like postboxes[3].postmessage(msgData);, the postmessage function emits the signal that is connected to the device[3].

              As far as I can tell, this is the only "correct/simple" way to do this because qt slots/signals does not seem to be setup to do message routing as such. Please someone correct me if I am wrong!

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 13 Oct 2015, 21:47 last edited by
                #7

                Hi,

                Maybe QSignalMapper could also be a solution

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

                C 1 Reply Last reply 14 Oct 2015, 06:13
                0
                • S SGaist
                  13 Oct 2015, 21:47

                  Hi,

                  Maybe QSignalMapper could also be a solution

                  C Offline
                  C Offline
                  code_fodder
                  wrote on 14 Oct 2015, 06:13 last edited by
                  #8

                  @SGaist
                  Thanks,

                  I looked into QSignalMapper, but it seems to have limitations, for example you can only pass one parameter and you can really just map one slot/signal.

                  I could probably make it work by passing a structure (and register that with the meta compiler), and then use multiple QSignalMappers... but, at least for my case, I find that creating my own object to be much simpler both to implement and to read.

                  1 Reply Last reply
                  0

                  1/8

                  12 Oct 2015, 13:38

                  • Login

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