It attempts to cast its argument to the pointer type specified in angle-brackets, returning a non-zero pointer if the object is of the correct type (determined at run-time), or 0 if the object's type is incompatible.
QObject *obj = new MyWidget;
QWidget
*widget = qobject_cast
MyWidget *myWidget =
qobject_cast
bool QObject::inherits( const char * className ) const;
QTimer *timer = new QTimer; // QTimer inherits QObject
timer->inherits("QTimer"); // returns true
timer->inherits("QObject"); // returns true
timer->inherits("QAbstractButton"); // returns false
This object is available as QObject::metaObject(). A single QMetaObject instance is created for each QObject subclass, and this instance stores all the meta-information for the QObject subclass.
QStringList properties;
for(int i=metaObject->propertyOffset();
i< metaObject->propertyCount(); ++i)
properties << QString::fromLatin1(metaObject->property(i).name());
The QMetaEnum class provides meta-data about an enumerator.
Declare new types with Q_DECLARE_METATYPE() to make them available to QVariant and other template-based functions. Call qRegisterMetaType() to make type available to non-template based functions, such as the queued signal and slot connections.
Q_DECLARE_METATYPE( Type )
This macro add the type Type to QMetaType. If Type has public default and copy constructors and a public destructor, the Q_DECLARE_METATYPE macro enables it to be used as a custom type in QVariant.
Ideally, this macro should be placed below the declaration of the class or struct.
{…};
MyStruct s;
QVariant var;
var.setValue(s); // copy s into the variant
// retrieve the value
MyStruct s2;
if
(s.canConvert
s2 =
s.value
n int qRegisterMetaType( const char * typeName );
Registers the type name typeName to the
type T. Returns the internal ID used by QMetaType. Any class or struct that has
a public constructor, a public copy constructor, and a public destructor can be
registered. A registered metatype must be already
declared with Q_DECLARE_METATYPE. To use type T with the
QObject::property() API, qRegisterMetaType
n int QMetaType::type( const char * typeName ) [static] // Returns a handle to the type called typeName, or 0 if there is no such type.
n const char* QMetaType::typeName( int type ) [static] // Returns the type name associated with the given type, or 0 if no matching type was found.
n
int qMetaTypeId() //
Returns the meta. type id of type T at compile time. For Example: int id =
qMetaTypeId
Q_PROPERTY(type name
READ getFunction
[WRITE setFunction]
[RESET resetFunction]
[DESIGNABLE bool]
[SCRIPTABLE bool]
[STORED bool]
[USER bool])
Q_PROPERTY(QDate date READ getDate WRITE setDate)
class StageControl:public QObject
{
Q_OBJECT
public:
enum AxisId{
Axis_X = 0x01, // 0x000001
Axis_Y = 0x02, // 0x000010
Axis_Z = 0x04, // 0x000100
Axis_R = 0x08, // 0x001000
Axis_T = 0x10 // 0x010000
};
Q_DECLARE_FLAGS(AxisIds, AxisId)
Q_FLAGS(AxisIds)
StageControl()
{
const QMetaObject * mobj = metaObject();
for (int i=mobj->enumeratorOffset(); i
{
QMetaEnum menum = mobj->enumerator(i);
qDebug()<<"name:
"<
for (int ii=0;
ii
qDebug()<
}
};
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/61367/viewspace-722405/,如需转载,请注明出处,否则将追究法律责任。