PDA

View Full Version : qobject_cast and sender()



ksrarc
23rd September 2009, 16:47
Hello,

Lets set this:



using namespace srps;
bool SRPSMain::initialize()
{
QObject * obj = new Species;
Species * sp = qobject_cast<Species*>( obj );
sp->setObjectName("sp");
qDebug() << obj << sp;
connect( sp, SIGNAL(destroyed()), this, SLOT(deleted()) );
sp->deleteLater();
return true;
}

void SRPSMain::deleted()
{
qDebug() << Q_FUNC_INFO << sender() << qobject_cast<Species*>( sender() );
}


both classes are QObject direct subclases and have Q_OBJECT macro.

the output is:



srps::Species(0x82ab00, name = "sp") srps::Species(0x82ab00, name = "sp")
void srps::SRPSMain::deleted() QObject(0x82ab00, name = "sp") QObject(0x0)


so, why the second qobject_cast does not work?

Thanks.

caduel
23rd September 2009, 16:57
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