Results 1 to 6 of 6

Thread: How to get sender in static SLOT

  1. #1
    Join Date
    Jul 2017
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to get sender in static SLOT

    We can define static slots .

    Qt Code:
    1. QMenu* getMenu(){
    2. QMenu* menu = new QMenu;
    3. auto action = menu->addAction("edit");
    4.  
    5. QObject::connect(action,&QAction::triggered,onClicked);
    6.  
    7. return menu;
    8. }
    9.  
    10. Q_SLOT static void onClicked(){
    11. throw;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Just I wonder that how to get whos the sender ? Is there a direct way to get it ?

  2. #2
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get sender in static SLOT

    Hello,

    In your slot:-
    Qt Code:
    1. QObject *senderObj = sender();
    2. if (senderObj == WHATEVER) {
    3. //code
    4. }
    To copy to clipboard, switch view to plain text mode 
    You should be able to manipulate senderObj, I don't know if this works in static slots.

    Regards

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get sender in static SLOT

    I don't know if this works in static slots.
    Almost certainly not. QObject::sender() is a protected, non-static method of the QObject class, so the only way you can call it is from within a member function of a class derived from QObject. The only way to call a non-static member function is through an instance of the class, and since a static method in most cases is called without an instance, there is no way to call sender().

    Just I wonder that how to get whos the sender ? Is there a direct way to get it ?
    I don't think so. In this case, the slot is nothing more than an ordinary function so there is no way in C++ that I know of to determine who is calling the function when you are inside it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. The following user says thank you to d_stranz for this useful post:

    qtplus (28th August 2017)

  5. #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: How to get sender in static SLOT

    I'd say that it doesn't make that much sense to have a static slot like that. You can instead use a lambda expression as a slot where the sender can be accessed directly.
    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.


  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get sender in static SLOT

    You can instead use a lambda expression as a slot where the sender can be accessed directly.
    Interesting. Are lambda expressions treated as if they were inline member functions of the class in which they are declared? Or are you suggesting that the pointer to the sender be given as one of the scope arguments to the lambda?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  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: How to get sender in static SLOT

    Yes, you need to pull in the relevant object into the closure.

    Qt Code:
    1. QObject::connect(action,&QAction::triggered,[action]() { action->doSomething(); });
    To copy to clipboard, switch view to plain text mode 
    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.


Similar Threads

  1. Deleting the signal sender from a slot
    By Tulon in forum Qt Programming
    Replies: 6
    Last Post: 29th November 2016, 01:22
  2. Invoking a slot from a static method in time
    By Momergil in forum Qt Programming
    Replies: 2
    Last Post: 29th August 2014, 12:51
  3. Replies: 4
    Last Post: 26th June 2014, 18:27
  4. Slot not visible after static cast
    By giantdragon in forum General Programming
    Replies: 5
    Last Post: 8th January 2011, 21:45
  5. Replies: 0
    Last Post: 15th December 2010, 06:18

Tags for this Thread

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.