Results 1 to 14 of 14

Thread: QObject::sender() in a Q_PRIVATE_SLOT

  1. #1
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QObject::sender() in a Q_PRIVATE_SLOT

    I have a private class with a slot I've implemented using Q_PRIVATE_SLOT. I need to know which object emitted the signal that is connected to the slot. Since MyClass is not a QObject and the slot is not really implemented in MyClass but in MyClassPrivate, is it safe to use q->sender()?

    Qt Code:
    1. class MyClassPrivate
    2. {
    3. public:
    4. ...
    5. void _q_mySlot()
    6. {
    7. Q_Q(MyClass);
    8. qDebug() << q->sender();
    9. // should print "foo(0x.....)"
    10. };
    11.  
    12. MyClass * const q_ptr;
    13. Q_DECLARE_PUBLIC(MyClass);
    14. };
    15.  
    16. class MyClass
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. MyClass(QObject *parent)
    22. : QObject(parent),
    23. d_ptr(new MyClassPrivate(this))
    24. {
    25. connect(foo, SIGNAL(bar()),
    26. this, SLOT(_q_mySlot()));
    27. };
    28.  
    29. Q_DECLARE_PRIVATE(MyClass);
    30. Q_PRIVATE_SLOT(d_func(), void _q_mySlot());
    31. MyClassPrivate * const d_ptr;
    32. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Ok, I answer to myself. I have found plenty of q->sender() calls in the Qt source code, so I guess it's safe.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    To see which object emitted the signal, you should really use QSignalMapper, not sender()

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Of couse QSignalMapper itself uses sender(), so...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    vfernandez (17th June 2010)

  6. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    It does, but it's more OO doing it that way, plus it's typically easier to use a simple integer rather than comparing pointers. For example, in a switch statement. The pointer would be non-const, where as the integer would be.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Quote Originally Posted by fatjuicymole View Post
    It does, but it's more OO doing it that way, plus it's typically easier to use a simple integer rather than comparing pointers. For example, in a switch statement. The pointer would be non-const, where as the integer would be.
    What about eventFilter()? There you have to compare pointers, I don't really see the difference...

    QSignalMapper is comparing pointers too so I don't see why one shouldn't have a map similar to the one in the signal mapper directly in his own class, it's always a couple of function calls less... And of course there is a question how to use the signal mapper if the signal we want to use already has some arguments (say... QFtp::commandFinished(int, bool)).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Sure, there are lots of classes in Qt that you can redesign yourself and place into your own code to save a couple of function calls, but why would you want to reinvent such functionality when it is already written? If the current class does not support some functionality (such as multiple arguments) then sure, make a better version, but otherwise, unless a profiling tool is telling you that the class is a bottleneck, then I see no reason not to use it.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Quote Originally Posted by fatjuicymole View Post
    Sure, there are lots of classes in Qt that you can redesign yourself and place into your own code to save a couple of function calls, but why would you want to reinvent such functionality when it is already written? If the current class does not support some functionality (such as multiple arguments) then sure, make a better version, but otherwise, unless a profiling tool is telling you that the class is a bottleneck, then I see no reason not to use it.
    Here is the situation - you can either use a single function call from a ready API or add a new object that does some stuff and part of that stuff you are actually interested in is calling the same function. Hmm... tough decision. It's like you wanted to hold a boolean value and used QString for that stating that QString was more object oriented and self-contained than bool.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Well, since we're talking about a slot in a private class, I'll go the q->sender() way. This makes my implementation really simple and easy to understand. The benefit of QSignalMapper doesn't make up for the overhead I'll get if I use it. Thanks for the suggestion anyway.

  11. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Quote Originally Posted by wysota View Post
    Here is the situation - you can either use a single function call from a ready API or add a new object that does some stuff and part of that stuff you are actually interested in is calling the same function. Hmm... tough decision. It's like you wanted to hold a boolean value and used QString for that stating that QString was more object oriented and self-contained than bool.
    So what your saying is that QSignalMapper is a waste of processor usage and people should always just use sender() instead? In which case, why was the QSignalMapper class created if it just bloats your code?

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Quote Originally Posted by fatjuicymole View Post
    So what your saying is that QSignalMapper is a waste of processor usage and people should always just use sender() instead?
    No.
    In which case, why was the QSignalMapper class created if it just bloats your code?
    It's useful if you are waiting for signals from more than one specific object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Ok, now I'm confused. The OP wanted to know which object emitted the signal that is connected to a particular slot. Assuming the slot had no arguments, why would you use sender() instead of QSignalMapper?

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Quote Originally Posted by fatjuicymole View Post
    Ok, now I'm confused. The OP wanted to know which object emitted the signal that is connected to a particular slot. Assuming the slot had no arguments, why would you use sender() instead of QSignalMapper?
    Here is an example:
    Qt Code:
    1. QTcpSocket *socket = ...;
    2. QBuffer *buffer = ...;
    3. connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
    4. connect(buffer, SIGNAL(readyRead()), this, SLOT(readData()));
    5.  
    6. void SomeClass::readData() {
    7. QIODevice *dev = qobject_cast<QIODevice*>(sender());
    8. if(!dev) return;
    9. qDebug() << dev->readAll();
    10. }
    To copy to clipboard, switch view to plain text mode 
    You want to know which object emitted the signal because you want to access it and using QSignalMapper for this would be a bit silly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::sender() in a Q_PRIVATE_SLOT

    Ah, now everything makes sense. Indeed, it would be silly to use QSignalMapper and then use the result to index into an array to get a pointer which you had in the first place!

    I didn't realise this is what the OP wanted to do, which is why I suggested QSignalMapper. I failed reading the first post.

Similar Threads

  1. qobject_cast and sender()
    By ksrarc in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2009, 16:57
  2. problem in sender()
    By wagmare in forum Qt Programming
    Replies: 13
    Last Post: 15th July 2009, 10:04
  3. [solved] Which object type is QObject::sender()?
    By ricardo in forum Qt Programming
    Replies: 6
    Last Post: 8th May 2009, 21:03
  4. tip on using QObject::sender()
    By drhex in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2008, 20:01
  5. question about the use of sender( ) function
    By luffy27 in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 08:15

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.