Results 1 to 2 of 2

Thread: qobject_cast and sender()

  1. #1
    Join Date
    Dec 2008
    Location
    Cali, Colombia
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation qobject_cast and sender()

    Hello,

    Lets set this:

    Qt Code:
    1. using namespace srps;
    2. bool SRPSMain::initialize()
    3. {
    4. QObject * obj = new Species;
    5. Species * sp = qobject_cast<Species*>( obj );
    6. sp->setObjectName("sp");
    7. qDebug() << obj << sp;
    8. connect( sp, SIGNAL(destroyed()), this, SLOT(deleted()) );
    9. sp->deleteLater();
    10. return true;
    11. }
    12.  
    13. void SRPSMain::deleted()
    14. {
    15. qDebug() << Q_FUNC_INFO << sender() << qobject_cast<Species*>( sender() );
    16. }
    To copy to clipboard, switch view to plain text mode 

    both classes are QObject direct subclases and have Q_OBJECT macro.

    the output is:

    Qt Code:
    1. srps::Species(0x82ab00, name = "sp") srps::Species(0x82ab00, name = "sp")
    2. void srps::SRPSMain::deleted() QObject(0x82ab00, name = "sp") QObject(0x0)
    To copy to clipboard, switch view to plain text mode 

    so, why the second qobject_cast does not work?

    Thanks.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qobject_cast and sender()

    probably: the nature of the deleted() signal is that it is emitted in QObject's destructor. By that time your subclass's destructor ~Species has been executed and the class is no longer a Species but only a QObject. Accessing any members of Species would *not* be correct anymore.

    So the cast does work. Your expectations are not correct.

    HTH

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

    ksrarc (23rd September 2009)

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.