PDA

View Full Version : [solved] Which object type is QObject::sender()?



ricardo
8th May 2009, 20:37
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);

Lykurg
8th May 2009, 20:45
simply cast sender() to QDockWidget and see if the pointer is valid.

jpn
8th May 2009, 20:50
Use qobject_cast.

Lykurg
8th May 2009, 20:50
QObject* sender = const_cast<QObject*>(QObject::sender());
QDockWidget* dock = static_cast<QDockWidget*>(sender);
you also can use the Qt way
QDockWidget* dock = qobject_cast<QDockWidget *>(QObject::sender());

ricardo
8th May 2009, 20:50
simply cast sender() to QDockWidget and see if the pointer is valid.

Do you mean !=NULL?

Lykurg
8th May 2009, 20:55
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


QDockWidget *dock = qobject_cast<QDockWidget *>(QObject::sender());
if(dock)
{
// sender is a QDockWidget
}


... 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:
if (myglobalpointer == QObject::sender())
//...

ricardo
8th May 2009, 21:03
From the docs:

So


QDockWidget *dock = qobject_cast<QDockWidget *>(QObject::sender());
if(dock)
{
// sender is a QDockWidget
}


... 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:
if (myglobalpointer == QObject::sender())
//...


Thanks a lot. Solved.