PDA

View Full Version : Signal from multipal sender



suneel1310
25th June 2013, 09:52
Hi all,

Is it possible to create a signal-slot connection, where the slot is connected to a specific signal from any sender.
i.e irrespective of who emits a particular signal, it should be handled in a same slot.
I have more than 5 distinct classes(and hence objects) which will emit a same signal. And I want to catch all these signals in a slot and handle it.

Lesiok
25th June 2013, 09:55
But what is the problem ? You can connect many signals to one slot.

suneel1310
25th June 2013, 10:04
Here signal is same, but senders are many.
Receiver does not know, who all may send the signal.

I was looking for something like below, [I am sure below connection doesn't work, since the sender is NULL]
connect(0, SIGNAL(addText(QString)), this, SLOT(append(QString)));

saman_artorious
25th June 2013, 10:59
Here signal is same, but senders are many.
Receiver does not know, who all may send the signal.


QObject::sender() will return you a pointer to signal sender when you call it from the slot.

anda_skoa
25th June 2013, 11:18
I was looking for something like below, [I am sure below connection doesn't work, since the sender is NULL]
connect(0, SIGNAL(addText(QString)), this, SLOT(append(QString)));

Yes, that will not work.
But you can do the connection from outside sender and receiver, i.e. in code that has access to both pointers.



QLabel *receiver = new QLabel(this);
QSpinBox *sender1 = new QSpinBox(this);
QSlider *sender2 = new QSlider(this);

connect(sender1, SIGNAL(valueChanged(int)), receiver, SLOT(setNum(int)));
connect(sender2, SIGNAL(valueChanged(int)), receiver, SLOT(setNum(int)));

Two senders of different class, same signal, same receiver and slot.

Cheers,
_

suneel1310
25th June 2013, 11:26
QLabel *receiver = new QLabel(this);
QSpinBox *sender1 = new QSpinBox(this);
QSlider *sender2 = new QSlider(this);

connect(sender1, SIGNAL(valueChanged(int)), receiver, SLOT(setNum(int)));
connect(sender2, SIGNAL(valueChanged(int)), receiver, SLOT(setNum(int)));
_

Yes, this will do.
But I didn't want to expose some of the pointers. !!
So I wanted to see if any way I can create a signal-slot connection with a generic sender.

anda_skoa
26th June 2013, 10:20
You can create a mediator if you want to, i.e. an object that has a signal of the same signature that you connect to the receiver.
Then you connect all senders to that mediator.

if with expose you mean that some of those will be internal to some other class, then you can forward a signal by using a signal/signal connection.

Cheers,
_

suneel1310
27th June 2013, 13:31
then you can forward a signal by using a signal/signal connection.


Yes, signal/signal connection would suit my purpose better.