ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Meta-Object System : From Custom Type To QVariant

Meta-Object System : From Custom Type To QVariant

原创 Linux操作系统 作者:rock1411 时间:2012-04-28 06:30:00 0 删除 编辑
Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 Meta-Object System

Features

  • QObject::metaObject() returns the associated meta-object for the class.
  • QMetaObject::className() returns the class name as a string at run-time.
  • QObject::inherits() function returns whether an object is an instance of a class that inherits a specified class within the QObject inheritance tree.
  • dynamic casts using qobject_cast() on QObject classes.

The qobject_cast() function

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(obj);

MyWidget *myWidget = qobject_cast(obj);

The inherits() function

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

QMetaObject Class

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.

  • className() returns the name of a class.
  • int enumeratorOffset() const;
  • int enumeratorCount() const;
  • QMetaEnum enumerator( int index ) const;
  • int indexOfEnumerator( const char * name ) const;
  • int propertyOffset() const;
  • int propertyCount() const; // Returns the number of properties in this class, including the number of properties provided by each base class.
  • QMetaProperty property( int index ) const;
  • int indexOfProperty( const char * name ) const;
const QMetaObject* metaObject = obj->metaObject();

QStringList properties;

for(int i=metaObject->propertyOffset();

i< metaObject->propertyCount(); ++i)

         properties << QString::fromLatin1(metaObject->property(i).name());

QMetaEnum Class

The QMetaEnum class provides meta-data about an enumerator.

  • const char* name() const; // the enumerator's name
  • int keyCount() const; // the number of keys.
  • const char* key( int index ) const; // The enumerator's keys (names of each enumerated item)
  • int value( int index ) const; // Returns the value with the given index;
  • int keyToValue( const char * key ) const;
  • int keysToValue( const char * keys ) const; // Returns the value derived from combining together the values of the keys using the OR operator, or -1 if keys is not defined. Note that the strings in keys must be '|'-separated.
  • QByteArray valueToKeys( int value ) const; // Returns a byte array of '|'-separated keys that represents the given value.
  • const char* valueToKey( int value ) const;
  • bool isFlag() const; // isFlag() returns whether the enumerator is meant to be used as a flag, meaning that its values can be combined using the OR operator.

QMetaProperty Class

  • const char* name() const;
  • QVariant::Type type() const;
  • int userType() const; // The return value is one of the values that are registered with QMetaType, or 0 if the type is not registered.
  • const char* typeName() const;
  • QVariant read( const QObject * object ) const; // Reads the property's value from the given object.
  • bool write( QObject * object, const QVariant & value ) const; // Writes value as the property's value to the given object.

QMetaType Class

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.

struct MyStruct

{…};

Q_DECLARE_METATYPE(MyStruct)

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() must be called before it is used, typically in the constructor of the class that uses T or must occur early in the main program

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();

QObject Class

Q_PROPERTY() macro

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(); ienumeratorCount(); ++i)

{

            QMetaEnum menum = mobj->enumerator(i);

            qDebug()<<"name: "<

            for (int ii=0; ii

                qDebug()<

    }

};

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/61367/viewspace-722405/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-03-20

  • 博文量
    5
  • 访问量
    9630