Signal from multipal sender
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.
Re: Signal from multipal sender
But what is the problem ? You can connect many signals to one slot.
Re: Signal from multipal sender
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)));
Re: Signal from multipal sender
Quote:
Originally Posted by
suneel1310
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.
Re: Signal from multipal sender
Quote:
Originally Posted by
suneel1310
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.
Code:
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,
_
Re: Signal from multipal sender
Quote:
Originally Posted by
anda_skoa
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.
Re: Signal from multipal sender
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,
_
Re: Signal from multipal sender
Quote:
Originally Posted by
anda_skoa
then you can forward a signal by using a signal/signal connection.
Yes, signal/signal connection would suit my purpose better.