Qt(2):MOC文件解析

在Qt下,从QObject派生的类都会生成moc_文件。

1. 比如现在有2个文件,qmyobject.h和qmyobject.cpp,它里面有一个类QMyObject,记为版本A

[cpp]  view plain  copy
 print ?
  1. class QMyObject : public QObject  
  2. {  
  3.     Q_OBJECT  
  4.   
  5. public:  
  6.     QMyObject(QObject *parent);  
  7.     ~QMyObject();          
  8. };  

然后我们给其添加一个信号和一个槽函数,即修改后的qmyobject.h的内容如下,记为版本B。

[cpp]  view plain  copy
 print ?
  1. class QMyObject : public QObject  
  2. {  
  3.     Q_OBJECT  
  4.   
  5. public:  
  6.     QMyObject(QObject *parent);  
  7.     ~QMyObject();  
  8.       
  9. signals:  
  10.     void signalFunc(double);  
  11.   
  12. protected slots:  
  13.     void slotFunc(double);  
  14. };  

下面给出2个这2个版本的moc文件

未添加signal/slot添加了signal/slot
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /**************************************************************************** 
  2. ** Meta object code from reading C++ file 'qmyobject.h' 
  3. ** 
  4. ** Created by: The Qt Meta Object Compiler version 67 (Qt 5.2.1) 
  5. ** 
  6. ** WARNING! All changes made in this file will be lost! 
  7. *****************************************************************************/  
  8.   
  9. #include "../../qmyobject.h"  
  10. #include <QtCore/qbytearray.h>  
  11. #include <QtCore/qmetatype.h>  
  12. #if !defined(Q_MOC_OUTPUT_REVISION)  
  13. #error "The header file 'qmyobject.h' doesn't include <QObject>."  
  14. #elif Q_MOC_OUTPUT_REVISION != 67  
  15. #error "This file was generated using the moc from 5.2.1. It"  
  16. #error "cannot be used with the include files from this version of Qt."  
  17. #error "(The moc has changed too much.)"  
  18. #endif  
  19.   
  20. QT_BEGIN_MOC_NAMESPACE  
  21. <span style="color:#ff0000;">struct qt_meta_stringdata_QMyObject_t {  
  22.     QByteArrayData data[1];  
  23.     char stringdata[11];  
  24. };</span>  
  25. #define QT_MOC_LITERAL(idx, ofs, len) \  
  26.     Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \  
  27.     offsetof(qt_meta_stringdata_QMyObject_t, stringdata) + ofs \  
  28.         - idx * sizeof(QByteArrayData) \  
  29.     )  
  30. <span style="color:#ff0000;">static const qt_meta_stringdata_QMyObject_t qt_meta_stringdata_QMyObject = {  
  31.     {  
  32. QT_MOC_LITERAL(0, 0, 9)  
  33.     },  
  34.     "QMyObject\0"  
  35. };</span>  
  36. #undef QT_MOC_LITERAL  
  37.   
  38. <span style="color:#ff0000;">static const uint qt_meta_data_QMyObject[] = {  
  39.   
  40.  // content:  
  41.        7,       // revision  
  42.        0,       // classname  
  43.        0,    0, // classinfo  
  44.        0,    0, // methods  
  45.        0,    0, // properties  
  46.        0,    0, // enums/sets  
  47.        0,    0, // constructors  
  48.        0,       // flags  
  49.        0,       // signalCount  
  50.   
  51.        0        // eod  
  52. };</span>  
  53.   
  54. <span style="color:#ff0000;">void QMyObject::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)  
  55. {  
  56.     Q_UNUSED(_o);  
  57.     Q_UNUSED(_id);  
  58.     Q_UNUSED(_c);  
  59.     Q_UNUSED(_a);  
  60. }</span>  
  61.   
  62. const QMetaObject QMyObject::staticMetaObject = {  
  63.     { &QObject::staticMetaObject, qt_meta_stringdata_QMyObject.data,  
  64.       qt_meta_data_QMyObject,  qt_static_metacall, 0, 0}  
  65. };  
  66.   
  67.   
  68. const QMetaObject *QMyObject::metaObject() const  
  69. {  
  70.     return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;  
  71. }  
  72.   
  73. void *QMyObject::qt_metacast(const char *_clname)  
  74. {  
  75.     if (!_clname) return 0;  
  76.     if (!strcmp(_clname, qt_meta_stringdata_QMyObject.stringdata))  
  77.         return static_cast<void*>(const_cast< QMyObject*>(this));  
  78.     return QObject::qt_metacast(_clname);  
  79. }  
  80.   
  81. <span style="color:#ff0000;">int QMyObject::qt_metacall(QMetaObject::Call _c, int _id, void **_a)  
  82. {  
  83.     _id = QObject::qt_metacall(_c, _id, _a);  
  84.     if (_id < 0)  
  85.         return _id;  
  86.     return _id;  
  87. }</span>  
  88. QT_END_MOC_NAMESPACE  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /**************************************************************************** 
  2. ** Meta object code from reading C++ file 'qmyobject.h' 
  3. ** 
  4. ** Created by: The Qt Meta Object Compiler version 67 (Qt 5.2.1) 
  5. ** 
  6. ** WARNING! All changes made in this file will be lost! 
  7. *****************************************************************************/  
  8.   
  9. #include "../../qmyobject.h"  
  10. #include <QtCore/qbytearray.h>  
  11. #include <QtCore/qmetatype.h>  
  12. #if !defined(Q_MOC_OUTPUT_REVISION)  
  13. #error "The header file 'qmyobject.h' doesn't include <QObject>."  
  14. #elif Q_MOC_OUTPUT_REVISION != 67  
  15. #error "This file was generated using the moc from 5.2.1. It"  
  16. #error "cannot be used with the include files from this version of Qt."  
  17. #error "(The moc has changed too much.)"  
  18. #endif  
  19.   
  20. QT_BEGIN_MOC_NAMESPACE  
  21. <span style="color:#ff0000;">struct qt_meta_stringdata_QMyObject_t {  
  22.     QByteArrayData data[4];  
  23.     char stringdata[32];  
  24. };</span>  
  25. #define QT_MOC_LITERAL(idx, ofs, len) \  
  26.     Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \  
  27.     offsetof(qt_meta_stringdata_QMyObject_t, stringdata) + ofs \  
  28.         - idx * sizeof(QByteArrayData) \  
  29.     )  
  30. <span style="color:#ff0000;">static const qt_meta_stringdata_QMyObject_t qt_meta_stringdata_QMyObject = {  
  31.     {  
  32. QT_MOC_LITERAL(0, 0, 9),  
  33. QT_MOC_LITERAL(1, 10, 10),  
  34. QT_MOC_LITERAL(2, 21, 0),  
  35. QT_MOC_LITERAL(3, 22, 8)  
  36.     },  
  37.     "QMyObject\0signalFunc\0\0slotFunc\0"  
  38. };</span>  
  39. #undef QT_MOC_LITERAL  
  40.   
  41. <span style="color:#ff0000;">static const uint qt_meta_data_QMyObject[] = {  
  42.   
  43.  // content:  
  44.        7,       // revision  
  45.        0,       // classname  
  46.        0,    0, // classinfo  
  47.        2,   14, // methods  
  48.        0,    0, // properties  
  49.        0,    0, // enums/sets  
  50.        0,    0, // constructors  
  51.        0,       // flags  
  52.        1,       // signalCount  
  53.   
  54.  // signals: name, argc, parameters, tag, flags  
  55.        1,    1,   24,    2, 0x06,  
  56.   
  57.  // slots: name, argc, parameters, tag, flags  
  58.        3,    1,   27,    2, 0x09,  
  59.   
  60.  // signals: parameters  
  61.     QMetaType::Void, QMetaType::Double,    2,  
  62.   
  63.  // slots: parameters  
  64.     QMetaType::Void, QMetaType::Double,    2,  
  65.   
  66.        0        // eod  
  67. };</span>  
  68.   
  69. <span style="color:#ff0000;">void QMyObject::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)  
  70. {  
  71.     if (_c == QMetaObject::InvokeMetaMethod) {  
  72.         QMyObject *_t = static_cast<QMyObject *>(_o);  
  73.         switch (_id) {  
  74.         case 0: _t->signalFunc((*reinterpret_castdouble(*)>(_a[1]))); break;  
  75.         case 1: _t->slotFunc((*reinterpret_castdouble(*)>(_a[1]))); break;  
  76.         default: ;  
  77.         }  
  78.     } else if (_c == QMetaObject::IndexOfMethod) {  
  79.         int *result = reinterpret_cast<int *>(_a[0]);  
  80.         void **func = reinterpret_cast<void **>(_a[1]);  
  81.         {  
  82.             typedef void (QMyObject::*_t)(double );  
  83.             if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&QMyObject::signalFunc)) {  
  84.                 *result = 0;  
  85.             }  
  86.         }  
  87.     }  
  88. }</span>  
  89.   
  90. const QMetaObject QMyObject::staticMetaObject = {  
  91.     { &QObject::staticMetaObject, qt_meta_stringdata_QMyObject.data,  
  92.       qt_meta_data_QMyObject,  qt_static_metacall, 0, 0}  
  93. };  
  94.   
  95.   
  96. const QMetaObject *QMyObject::metaObject() const  
  97. {  
  98.     return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;  
  99. }  
  100.   
  101. void *QMyObject::qt_metacast(const char *_clname)  
  102. {  
  103.     if (!_clname) return 0;  
  104.     if (!strcmp(_clname, qt_meta_stringdata_QMyObject.stringdata))  
  105.         return static_cast<void*>(const_cast< QMyObject*>(this));  
  106.     return QObject::qt_metacast(_clname);  
  107. }  
  108.   
  109. <span style="color:#ff0000;">int QMyObject::qt_metacall(QMetaObject::Call _c, int _id, void **_a)  
  110. {  
  111.     _id = QObject::qt_metacall(_c, _id, _a);  
  112.     if (_id < 0)  
  113.         return _id;  
  114.     if (_c == QMetaObject::InvokeMetaMethod) {  
  115.         if (_id < 2)  
  116.             qt_static_metacall(this, _c, _id, _a);  
  117.         _id -= 2;  
  118.     } else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {  
  119.         if (_id < 2)  
  120.             *reinterpret_cast<int*>(_a[0]) = -1;  
  121.         _id -= 2;  
  122.     }  
  123.     return _id;  
  124. }</span>  
  125.   
  126. <strong><span style="color:#ff0000;">// SIGNAL 0  
  127. void QMyObject::signalFunc(double _t1)  
  128. {  
  129.     void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };  
  130.     QMetaObject::activate(this, &staticMetaObject, 0, _a);  
  131. }</span></strong>  
  132. QT_END_MOC_NAMESPACE  

在右边一栏:

qt_meta_stringdata_QMyObject_t:

代表QMyObject中的函数名。


qt_meta_data_QMyObject:

methods部分前面的2代表此类中信号和槽总数为2,14代表qt_meta_data_QMyObject中的content的个数,一共有14个,对应着QMetaObjectPrivate变量的14个成员变量。 

信号前面的是1,而槽前面的是3,在看看

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="color:#ff0000;">"QMyObject\0signalFunc\0\0slotFunc\0"</span>  

我们如果以\0作为索引,一对比就知道signalFunc的索引为1,而slotFunc的索引为3。


然后看看源对象定义:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. const QMetaObject myClass::staticMetaObject = {  
  2.     { &QObject::staticMetaObject, qt_meta_stringdata_myClass,  
  3.       qt_meta_data_myClass, 0 }  
  4. };  
  5. 可以看到源对象的数据定义为:  
  6.     struct { // private data  
  7.         const QMetaObject *superdata;  
  8.         const char *stringdata;  
  9.         const uint *data;  
  10.         const void *extradata;  
  11.     }  









参考:

1. http://blog.csdn.net/smad732/article/details/6107448#




FROM: http://blog.csdn.net/oldmtn/article/details/46346625

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值