当前位置:网站首页>[QT notes] simple understanding of QT meta object system

[QT notes] simple understanding of QT meta object system

2022-06-27 06:09:00 Lin Qi sevenlin

Meta object system

QT The meta object system of provides :

  • Signal and slot
  • Runtime type information
  • Dynamic attribute system

The meta object system is implemented based on the following three points :

  • Use QObject As the base class
  • Using macros in classes Q_OBJECT To turn on meta object properties , Such as : Dynamic properties 、 Signals and slots
  • Using the meta object compiler (moc) For every one QObject Subclasses provide the necessary code to implement meta object features

The meta object compiler is compiling C++ Source code , If the source code contains Q_OBJECT macro , Then it will generate a new source code with meta object code information

 

Features provided by the meta object system

  • Signal and slot
  • QObject::metaObject() Return the meta object of the class
  • QMetaObject::className() Returns the class name as a string at run time , Unwanted C++ Compiler vs native RTTI Support for
  • QObject::inherits() Function returns whether the object is inherited QObject An instance of a class of a specified class in the inheritance tree .
  • QObject::tr() and QObject::trUtf8() International translation string
  • QObject::setProperty and QObject::property() Dynamically set and get properties according to their names
  • QMetaObject::newInstance() Construct an instance of a new class

stay QObject Class , have access to qobject_cast() Perform dynamic conversion ,QT Medium qobject_cast() Similar to the standard C++ Medium dynamic_cast(), Its advantage is that no RTTI Support and work across dynamic library boundaries . If the type conversion succeeds , Returns a non-zero pointer ; If the conversion fails , Then return to nullptr

for instance :

const QMetaObject *QObject::metaObject() const
const char *QMetaObject::className() const

  QObject *obj = new QPushButton;
  obj->metaObject()->className();             // returns "QPushButton"

  QPushButton::staticMetaObject.className();  // returns "QPushButton"

 

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

 

bool QObject::setProperty(const char *name, const QVariant &value)

  QTimer *timer = new QTimer;
  timer->setProperty("interval", 1000);

 

T qobject_cast(const QObject *object)

  QObject *obj = new QTimer;  // QTimer Inherited from QObject
  QTimer *timer = qobject_cast<QTimer *>(obj);  //  Type conversion succeeded 

  QPushButton *btn = qobject_cast<QPushButton *>(obj);  // Type conversion failed ,QPushButton On QTimer incompatible types 
  //  therefore ,btn == nullptr

Learning materials

  • QT Help document
原网站

版权声明
本文为[Lin Qi sevenlin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270546236030.html