您的位置:首页 > 编程语言 > Qt开发

qt5.5.1 如何判断某个对象是否属于qt类中实例

2015-11-28 13:56 513 查看
根据qt5帮助文档,

bool QObject::inherits(const char * className) const

Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.

返回真,如果对象是指定的继承类或QObject类的子类的一个实例,就返真.否则返回甲.

Example:

QTimer *timer = new QTimer; // QTimer inherits QObject

timer->inherits("QTimer"); // returns true

timer->inherits("QObject"); // returns true

timer->inherits("QAbstractButton"); // returns false

// QVBoxLayout inherits QObject and QLayoutItem

QVBoxLayout *layout = new QVBoxLayout;

layout->inherits("QObject"); // returns true

layout->inherits("QLayoutItem"); // returns true (even though QLayoutItem is not a QObject)

如果您需要确定对象是否为特定类的实例, 建议用 qobject_cast<Type *>(object) 代替.

See also metaObject() and qobject_cast().
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: