Results 1 to 7 of 7

Thread: [solved] Which object type is QObject::sender()?

  1. #1
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Wink [solved] Which object type is QObject::sender()?

    Hi mates!

    When I receive a signal, I'd like to know in my SLOT if the sender is a QDockWidget, how can I test which kind of object is QObject::sender()?

    There is a method called isA but I guess is obsolete because is not available on qt4.5

    Any idea?
    Thanks in advance.


    EDIT:

    BTW, Why doesn't work this:
    QDockWidget* dock=(QDockWidget*) QObject::sender();
    But this works perfectly:
    QObject* sender = const_cast<QObject*>(QObject::sender());
    QDockWidget* dock = static_cast<QDockWidget*>(sender);
    Last edited by ricardo; 8th May 2009 at 21:08.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Which object type is QObject::sender()?

    simply cast sender() to QDockWidget and see if the pointer is valid.

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

    ricardo (8th May 2009)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Which object type is QObject::sender()?

    Use qobject_cast.
    J-P Nurmi

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

    ricardo (8th May 2009)

  6. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Which object type is QObject::sender()?

    Quote Originally Posted by ricardo View Post
    QObject* sender = const_cast<QObject*>(QObject::sender());
    QDockWidget* dock = static_cast<QDockWidget*>(sender);
    you also can use the Qt way
    Qt Code:
    1. QDockWidget* dock = qobject_cast<QDockWidget *>(QObject::sender());
    To copy to clipboard, switch view to plain text mode 

  7. The following 3 users say thank you to Lykurg for this useful post:

    perfectcircle (20th July 2013), ricardo (8th May 2009), thanyaj (20th December 2010)

  8. #5
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Which object type is QObject::sender()?

    Quote Originally Posted by Lykurg View Post
    simply cast sender() to QDockWidget and see if the pointer is valid.
    Do you mean !=NULL?

  9. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Which object type is QObject::sender()?

    Quote Originally Posted by ricardo View Post
    Do you mean !=NULL?
    From the docs:
    T qobject_cast ( QObject * object )
    Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns 0.
    So

    Qt Code:
    1. QDockWidget *dock = qobject_cast<QDockWidget *>(QObject::sender());
    2. if(dock)
    3. {
    4. // sender is a QDockWidget
    5. }
    To copy to clipboard, switch view to plain text mode 

    ... if you only dealing with a small number of dock widgets in you application and the slot has a pointer to them you can also of course use:
    Qt Code:
    1. if (myglobalpointer == QObject::sender())
    2. //...
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to Lykurg for this useful post:

    ricardo (8th May 2009)

  11. #7
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Which object type is QObject::sender()?

    Quote Originally Posted by Lykurg View Post
    From the docs:

    So

    Qt Code:
    1. QDockWidget *dock = qobject_cast<QDockWidget *>(QObject::sender());
    2. if(dock)
    3. {
    4. // sender is a QDockWidget
    5. }
    To copy to clipboard, switch view to plain text mode 

    ... if you only dealing with a small number of dock widgets in you application and the slot has a pointer to them you can also of course use:
    Qt Code:
    1. if (myglobalpointer == QObject::sender())
    2. //...
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot. Solved.

Similar Threads

  1. Replies: 4
    Last Post: 19th February 2009, 11:10
  2. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  3. Getting type of object
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 29th November 2007, 14:38
  4. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38
  5. Replies: 3
    Last Post: 15th April 2007, 19:16

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.