Map signal to slots in vector of widgets



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


  • Lifetime Qt Champion

    Hi,

    How do you decide which widget should receive the signal ?

    [edit: corrected unclear question… SGaist]



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


  • Lifetime Qt Champion

    I've rephrased my original not very readable question.



  • 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).


  • Lifetime Qt Champion

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


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.