Results 1 to 8 of 8

Thread: Signal from multipal sender

  1. #1
    Join Date
    Nov 2008
    Location
    Bangalore, India
    Posts
    21
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Symbian S60

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

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,478
    Thanked 274 Times in 269 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal from multipal sender

    But what is the problem ? You can connect many signals to one slot.

  3. #3
    Join Date
    Nov 2008
    Location
    Bangalore, India
    Posts
    21
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Symbian S60

    Default 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)));

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Signal from multipal sender

    Quote Originally Posted by suneel1310 View Post
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal from multipal sender

    Quote Originally Posted by suneel1310 View Post
    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.

    Qt Code:
    1. QLabel *receiver = new QLabel(this);
    2. QSpinBox *sender1 = new QSpinBox(this);
    3. QSlider *sender2 = new QSlider(this);
    4.  
    5. connect(sender1, SIGNAL(valueChanged(int)), receiver, SLOT(setNum(int)));
    6. connect(sender2, SIGNAL(valueChanged(int)), receiver, SLOT(setNum(int)));
    To copy to clipboard, switch view to plain text mode 
    Two senders of different class, same signal, same receiver and slot.

    Cheers,
    _

  6. #6
    Join Date
    Nov 2008
    Location
    Bangalore, India
    Posts
    21
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Symbian S60

    Default Re: Signal from multipal sender

    Quote Originally Posted by anda_skoa View Post
    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    suneel1310 (27th June 2013)

  9. #8
    Join Date
    Nov 2008
    Location
    Bangalore, India
    Posts
    21
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Symbian S60

    Default Re: Signal from multipal sender

    Quote Originally Posted by anda_skoa View Post
    then you can forward a signal by using a signal/signal connection.
    Yes, signal/signal connection would suit my purpose better.

Similar Threads

  1. Replies: 3
    Last Post: 20th April 2011, 15:42
  2. Replies: 0
    Last Post: 15th December 2010, 06:18
  3. Replies: 3
    Last Post: 30th July 2010, 16:34
  4. qobject_cast and sender()
    By ksrarc in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2009, 16:57
  5. tip on using QObject::sender()
    By drhex in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2008, 20:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.