文档:
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. If object is 0 then it will also return 0.
The class T must inherit (directly or indirectly) QObject and be declared with the Q_OBJECT macro.
A class is considered to inherit itself.
Example:
QObject *obj = new QTimer; // QTimer inherits QObject
QTimer *timer = qobject_cast(obj);
// timer == (QObject *)obj
QAbstractButton *button = qobject_cast(obj);
// button == 0
The qobject_cast() function behaves similarly to the standard C++ dynamic_cast(), with the advantages that it doesn't require RTTI support and it works across dynamic library boundaries.
qobject_cast() can also be used in conjunction with interfaces; see the Plug & Paint example for details.
翻译:
T qobject_cast(qobject*o