简体   繁体   English

Q_OBJECT和moc用于继承

[英]Q_OBJECT and moc for inheritance

I am working in a project where a Q_OBJECT macro is used in a class which I need to inherit. 我正在一个项目中工作,在我需要继承的类中使用Q_OBJECT宏。

The class where Q_OBJECT is already defined looks like this, 已定义Q_OBJECT的类看起来像这样,

class cBaseObject : public QObject, public cinformation
{
    Q_OBJECT
    //...
    //...
}

I am creating a new class via public inheritance of cBaseObject. 我正在通过cBaseObject的公共继承创建一个新类。 Should I need to write Q_OBJECT macro again? 我是否需要再次编写Q_OBJECT宏? I tried with and without that macro and I am seeing no moc_XXX.cxx file is generated if I didn't include the QT_MACRO 我尝试使用和不使用该宏,我发现如果我没有包含QT_MACRO,则不会生成moc_XXX.cxx文件

class cEnhancedbaseObject : public cBaseObject
{
    Q_OBJECT   // if i didn't include this 
          //no moc__XXX.cxx file is  generated
} 

But when I inherit the class why the Q_OBJECT macro's functionality is also not inherited. 但是当我继承类时,为什么Q_OBJECT宏的功能也没有被继承。 Is it any problem if Q_OBJECT macro is defined twice, if it is inherited? 如果Q_OBJECT宏被定义两次,如果它被继承,是否有任何问题? How does the behavior of Q_OBJECT for multilevel inheritance. Q_OBJECT的行为如何进行多级继承。 I have read that for multiple inheritance the QOBJECT class should be placed first. 我已经读过,对于多重继承,QOBJECT类应该放在第一位。 Is there anything similar for multilevel inheritance. 多级继承有什么类似的东西吗?

The presence of the Q_OBJECT macro marks the class for inclusion in Qt's meta-object system. Q_OBJECT宏的存在标记了包含在Qt的元对象系统中的类。 If you want your class to have its own identity in this meta-object system, you must put the Q_OBJECT macro into it (and make sure it is directly or indirectly derived from QObject , naturally). 如果您希望您的类在此元对象系统中具有自己的标识,则必须将Q_OBJECT宏放入其中(并确保它直接或间接地从QObject派生,当然)。

In your case of cBaseObject and cEnhancedbaseObject , if cEnhancedbaseObject does not include the Q_OBJECT macro, it will still work normally. cBaseObjectcEnhancedbaseObject情况下,如果cEnhancedbaseObject不包含Q_OBJECT宏,它仍然可以正常工作。 However, as far Qt's meta-object system is concerned, objects of type cEnhancedbaseObject will be of meta-type cBaseObject . 但是,就Qt的元对象系统而言, cEnhancedbaseObject类型的cEnhancedbaseObject将是元类型的cBaseObject You can see that using such functions as myObject->metaObject()->className() . 您可以看到使用myObject->metaObject()->className()等函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM