Results 1 to 15 of 15

Thread: is there anyway to know witch object sends a signal to a slot?

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default is there anyway to know witch object sends a signal to a slot?

    Hi

    There are two objects of a class(obj1 , obj2). These objects use same signal and slot like this:
    Qt Code:
    1. connect(obj1, SIGNAL(sgnl()), SLOT(slt()));
    2. connect(obj2, SIGNAL(sgnl()), SLOT(slt()));
    3.  
    4. //sgnl and slt are created by myself.
    To copy to clipboard, switch view to plain text mode 

    I need when sgnl() signal is send from obj1, the slt() slot can know this and use obj1 address and also when sgnl() signal is send from obj2, the slt() slot can know this and use obj2 address.

    Thanks for any helps.
    Last edited by Alex22; 3rd December 2015 at 12:49.

  2. #2
    Join Date
    Nov 2015
    Posts
    41
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?


  3. The following user says thank you to Scope for this useful post:

    Alex22 (8th January 2016)

  4. #3
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    Qt Code:
    1. void MyClass::slt(){
    2. Object* obj = (QObject*) sender(); //sender() holds the instance of the sender
    3. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Qiieha for this useful post:

    Alex22 (3rd December 2015)

  6. #4
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    Quote Originally Posted by Qiieha View Post
    Qt Code:
    1. void MyClass::slt(){
    2. Object* obj = (QObject*) sender(); //sender() holds the instance of the sender
    3. }
    To copy to clipboard, switch view to plain text mode 
    Realy thanks Qiieha, this works so nice!!!
    but we cast it to Object* and not to QObject*.

    Object* obj = (Object*) sender()
    Last edited by Alex22; 3rd December 2015 at 16:42.

  7. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    Yes, QObject::sender() works but it really is a hack that defeats modularity. QSignalMapper is the Right Way.

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

    Alex22 (3rd December 2015)

  9. #6
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    Quote Originally Posted by yeye_olive View Post
    Yes, QObject::sender() works but it really is a hack that defeats modularity. QSignalMapper is the Right Way.
    @yeye_olive, what do you mean about hack and modularity defeating?

  10. #7
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    You're welcome. yeye_olive is right. The doc of QObject::sender() says the same. You'll get in troubles, if you use this method in a multi-threaded program using direct connections. So it's safer and better to use QSignalMapper.

    Look at the doc of QSignalMapper. There's an example, which solves the same thing as you want to achieve.
    Last edited by Qiieha; 3rd December 2015 at 19:39.

  11. The following user says thank you to Qiieha for this useful post:

    Alex22 (3rd December 2015)

  12. #8
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    The documentation says it all:
    This function violates the object-oriented principle of modularity.
    The whole point of signals and slots is that a QObject need not worry if/how it is connected to other QObjects. The user calling QObject::connect() is the one whose program logic depends on the connections. QObject::sender() violates this principle because the behavior of a QObject depends on which object emitted the signal connected to its slot, which limits its reusability as a component. Everything the receiving QObject needs to know to execute its slots should be encoded in the slot's parameters, not hidden in sender(). QSignalMapper allows you to do just that.

  13. The following user says thank you to yeye_olive for this useful post:

    Alex22 (3rd December 2015)

  14. #9
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    So much thanks!!!

  15. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    And even if you cast, don't ever use C-Style casts in C++ programs.

    Cheers,
    _

  16. #11
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    Quote Originally Posted by anda_skoa View Post
    And even if you cast, don't ever use C-Style casts in C++ programs.

    Cheers,
    _
    @anda_skoa, Thanks for your helping but I can not understand what you mean. I used QSignalMapper and I need to cast it. Is there any problem by casting like this:

    Qt Code:
    1. QSignalMapper *_signalmapper = new QSignalMapper(this);
    2. .
    3. .
    4. .
    5. QComboBox * cmb = new QComboBox(this);
    6. cmb = (QComboBox*)(_signalmapper->mapping(4));
    To copy to clipboard, switch view to plain text mode 

  17. #12
    Join Date
    Nov 2015
    Posts
    41
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    What you doing? Please look for example in doc QSignalMapper

    Qt Code:
    1. ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. signalMapper = new QSignalMapper(this);
    5.  
    6. QGridLayout *gridLayout = new QGridLayout;
    7. for (int i = 0; i < texts.size(); ++i) {
    8. QPushButton *button = new QPushButton(texts[i]);
    9. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    10. signalMapper->setMapping(button, texts[i]);
    11. gridLayout->addWidget(button, i / 3, i % 3);
    12. }
    13.  
    14. connect(signalMapper, SIGNAL(mapped(QString)),
    15. this, SIGNAL(clicked(QString)));
    16.  
    17. setLayout(gridLayout);
    18. }
    To copy to clipboard, switch view to plain text mode 

    adjust as needed.

    http://doc.qt.io/qt-5/qsignalmapper.html

  18. #13
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    @Scope, I need to know which signal sends each time. by _signalmapper->mapping() I can know (this returns its address but must be casted).
    @anda_skoa@ But has told something about casting that i could not understand. Now my project is working by QSignalmapper and is like that example. anyway thanks for your answering

  19. #14
    Join Date
    Nov 2015
    Posts
    41
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    Ok, you should use http://doc.qt.io/qt-5/qobject.html#qobject_cast for QObject class or class which derived from QObject.

    In other case use c++ style cast like as static_cast - is safer.

  20. The following user says thank you to Scope for this useful post:

    Alex22 (4th December 2015)

  21. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: is there anyway to know witch object sends a signal to a slot?

    Quote Originally Posted by Alex22 View Post
    @anda_skoa, Thanks for your helping but I can not understand what you mean.
    Don't use C casts, i.e.
    Qt Code:
    1. ResultType *result = (ResultType*)input;
    To copy to clipboard, switch view to plain text mode 

    If you are absolutely certain that input is of type ResultType, then use a static_cast
    Qt Code:
    1. ResultType *result = static_cast<ResultType*>(input);
    To copy to clipboard, switch view to plain text mode 

    If you are not certain, use a checking cast, e.g. a qobject_cast for QObject based classed or dynamic_cast in general.
    Both will check the validity of the cast and return a null pointer if the cast is not possible.

    Cheers,
    _

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

    Alex22 (5th December 2015)

Similar Threads

  1. Replies: 1
    Last Post: 14th August 2014, 17:08
  2. SIGNAL/SLOT - Object scope?
    By StarShaper in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2012, 13:35
  3. Replies: 3
    Last Post: 3rd May 2009, 14:15
  4. Manually send signal to slot
    By donmorr in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2006, 15:03
  5. Signal-Slot, object instances
    By ct in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2006, 19:08

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.