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. Map signal to slots in vector of widgets

Map signal to slots in vector of widgets

Scheduled Pinned Locked Moved General and Desktop
signal & slot
7 Posts 4 Posters 2.7k Views 4 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.
  • O Offline
    O Offline
    oracle3001
    wrote on last edited by oracle3001
    #1

    Say I a widget A and a vector of widget B's (the number of which is only known at runtime and is dynamic)

    I would like a signal / slot relationship between A and all B's, such that I can choose the slot of which of the widget B's recieves the signal.

    i.e some sort of emit widgetASignal (slotOfWidgetB_X, double amount)

    Now, I am aware of QSignalMapper, but as far as I can tell that is for the counterpart of what I would like i.e QSignalMaper would be vector of widget B's, one of which sends a signal, and widget A slot recieves the signal and can work out which of widget B's it came from.

    I want to do the opposite here, and emit a signal defined in widgetA and it get to only one of the instances of widgetB.

    Any help much appreciated.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      How do you decide which widget should receive the signal ?

      [edit: corrected unclear question… SGaist]

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

      1 Reply Last reply
      0
      • O Offline
        O Offline
        oracle3001
        wrote on last edited by
        #3

        Yes, I want to decide which widget receives the signal.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I've rephrased my original not very readable question.

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

          1 Reply Last reply
          0
          • L Offline
            L Offline
            LuGRU
            wrote on last edited by
            #5

            Maybe connect signals from all objects to same slot and in slot do something like this:

            TargetClass* target1 = new TargetClass();
            TargetClass* target2 = new TargetClass();
            
            connect (target1, SIGNAL( doSomething()), this, SLOT( onDoSomething()));
            connect (target2, SIGNAL( doSomething()), this, SLOT( onDoSomething()));
            

            inside onDoSomething

            TargetClass *targe = static_cast<TargetClass*>( this->sender());
            target->doSomething();
            

            Also if You are working with Ui classes (widgets) then parent->findChildrens will come in handy (i.e. to get all widgets for the parent and i.e. uncheck all checkboxes except for the current one - although this con be done purely using proper widgets).

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Don't use static_cast with QObject derived classes, there's qobject_cast for that

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

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                I think there's a deeper problem here. Conceptual one.
                The object emitting a signal is not suppose to know anything about the receivers. It's only role is to signal something. Who and how reacts to it should not be of its concern.
                So conceptually at least it's not a case where you would use a signal/slot mechanism. In your case object A is perfectly aware of the existence and placement of objects B, so it might just as well call their method directly.

                If you insist on using a signal/slot here I would create a manager object for the vector, emit something like signalFromA(int index, double amount) and in the slot of the manager simply do something like slotOfManager(int index, double amount) { vectorOfBs[index]->doStuff(amount); }, but again, that really doesn't look like a job for signals/slots. You might as well hold a pointer (or a ref) to the vector in the A and call doStuff of wanted B directly.

                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