qobject_cast

template
inline T qobject_cast(QObject *object)
{
    typedef typename std::remove_cv::type>::type ObjType;
    Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro::Value,
                    "qobject_cast requires the type to have a Q_OBJECT macro");
    return static_cast(ObjType::staticMetaObject.cast(object));
}

template
inline T qobject_cast(const QObject *object)
{
    typedef typename std::remove_cv::type>::type ObjType;
    Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro::Value,
                      "qobject_cast requires the type to have a Q_OBJECT macro");
    return static_cast(ObjType::staticMetaObject.cast(object));
}

T qobject_cast函数是QObject 类中的函数,它将object对象指针转换为T类型的对象指针,T类型必须是直接或间接继承自QObject 类,而且在其定义中要有Q_OBJECT宏变量。

你可能感兴趣的:(Qt,c++)