16,239
社区成员
发帖
与我相关
我的任务
分享
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<QTimer *>(obj);
// timer == (QObject *)obj
QAbstractButton *button = qobject_cast<QAbstractButton *>(obj);
// button == 0
QObject *obj = new QTimer; // QTimer inherits QObject
QTimer* timer = qobject_cast<QTimer*>(obj); // 这里就是下行转换
// timer == (QObject *)obj
QAbstractButton* button = qobject_cast<QAbstractButton*>(obj);
// button == 0
QPushButton* buttonObj = new QPushButton;
button = qobject_cast<QAbstractButton*>(buttonObj); // 这里就是上行转换,不存在是否抽象之说