QMetaObject编译器解释一下?
我想知道Qt如何实现元对象编译,使他们能够在Qt中提供信号槽机制。我无法在文档中找到很多详细信息。
编辑::我无法获取 _id = QObject::qt_metacall(_c, _id, _a);
的源代码
以下是源文件和相应的 moc 文件。
#ifndef SSOBJECT_H
#define SSOBJECT_H
#include <QObject>
class ssObject : public QObject
{
Q_OBJECT
public:
explicit ssObject(QObject *parent = 0);
signals:
void readyToPrint();
void readyToPrint1(int);
void readyToPrint2(float);
private slots:
int print();
};
#endif // SSOBJECT_H
//output from MOC
#include "../ssobject.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'ssobject.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 62
#error "This file was generated using the moc from 4.6.1. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif
QT_BEGIN_MOC_NAMESPACE
static const uint qt_meta_data_ssObject[] = {
// content:
4, // revision
0, // classname
0, 0, // classinfo
4, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
3, // signalCount
// signals: signature, parameters, type, tag, flags
10, 9, 9, 9, 0x05,
25, 9, 9, 9, 0x05,
44, 9, 9, 9, 0x05,
// slots: signature, parameters, type, tag, flags
69, 9, 65, 9, 0x08,
0 // eod
};
static const char qt_meta_stringdata_ssObject[] = {
"ssObject\0\0readyToPrint()\0readyToPrint1(int)\0"
"readyToPrint2(float)\0int\0print()\0"
};
const QMetaObject ssObject::staticMetaObject = {
{ &QObject::staticMetaObject, qt_meta_stringdata_ssObject,
qt_meta_data_ssObject, 0 }
};
#ifdef Q_NO_DATA_RELOCATION
const QMetaObject &ssObject::getStaticMetaObject() { return staticMetaObject; }
#endif //Q_NO_DATA_RELOCATION
const QMetaObject *ssObject::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}
void *ssObject::qt_metacast(const char *_clname)
{
if (!_clname) return 0;
if (!strcmp(_clname, qt_meta_stringdata_ssObject))
return static_cast<void*>(const_cast< ssObject*>(this));
return QObject::qt_metacast(_clname);
}
int ssObject::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QObject::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
switch (_id) {
case 0: readyToPrint(); break;
case 1: readyToPrint1((*reinterpret_cast< int(*)>(_a[1]))); break;
case 2: readyToPrint2((*reinterpret_cast< float(*)>(_a[1]))); break;
case 3: { int _r = print();
if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; } break;
default: ;
}
_id -= 4;
}
return _id;
}
// SIGNAL 0
void ssObject::readyToPrint()
{
QMetaObject::activate(this, &staticMetaObject, 0, 0);
}
// SIGNAL 1
void ssObject::readyToPrint1(int _t1)
{
void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 1, _a);
}
// SIGNAL 2
void ssObject::readyToPrint2(float _t1)
{
void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 2, _a);
}
QT_END_MOC_NAMESPACE
同时解释一下如何 QObject::connect(ssobj1,SIGNAL(readyToPrint()),ssobj1,SLOT(print())) 展开
I am trying to know how Qt implements meta object compiling which enables them to provide signal slot mechanism in Qt. I am not able to finding much details in the documentation.
EDIT::I am not able to get the source code of _id = QObject::qt_metacall(_c, _id, _a);
Following is source file and corresponding moc file.
#ifndef SSOBJECT_H
#define SSOBJECT_H
#include <QObject>
class ssObject : public QObject
{
Q_OBJECT
public:
explicit ssObject(QObject *parent = 0);
signals:
void readyToPrint();
void readyToPrint1(int);
void readyToPrint2(float);
private slots:
int print();
};
#endif // SSOBJECT_H
//output from MOC
#include "../ssobject.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'ssobject.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 62
#error "This file was generated using the moc from 4.6.1. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif
QT_BEGIN_MOC_NAMESPACE
static const uint qt_meta_data_ssObject[] = {
// content:
4, // revision
0, // classname
0, 0, // classinfo
4, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
3, // signalCount
// signals: signature, parameters, type, tag, flags
10, 9, 9, 9, 0x05,
25, 9, 9, 9, 0x05,
44, 9, 9, 9, 0x05,
// slots: signature, parameters, type, tag, flags
69, 9, 65, 9, 0x08,
0 // eod
};
static const char qt_meta_stringdata_ssObject[] = {
"ssObject\0\0readyToPrint()\0readyToPrint1(int)\0"
"readyToPrint2(float)\0int\0print()\0"
};
const QMetaObject ssObject::staticMetaObject = {
{ &QObject::staticMetaObject, qt_meta_stringdata_ssObject,
qt_meta_data_ssObject, 0 }
};
#ifdef Q_NO_DATA_RELOCATION
const QMetaObject &ssObject::getStaticMetaObject() { return staticMetaObject; }
#endif //Q_NO_DATA_RELOCATION
const QMetaObject *ssObject::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}
void *ssObject::qt_metacast(const char *_clname)
{
if (!_clname) return 0;
if (!strcmp(_clname, qt_meta_stringdata_ssObject))
return static_cast<void*>(const_cast< ssObject*>(this));
return QObject::qt_metacast(_clname);
}
int ssObject::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QObject::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
switch (_id) {
case 0: readyToPrint(); break;
case 1: readyToPrint1((*reinterpret_cast< int(*)>(_a[1]))); break;
case 2: readyToPrint2((*reinterpret_cast< float(*)>(_a[1]))); break;
case 3: { int _r = print();
if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; } break;
default: ;
}
_id -= 4;
}
return _id;
}
// SIGNAL 0
void ssObject::readyToPrint()
{
QMetaObject::activate(this, &staticMetaObject, 0, 0);
}
// SIGNAL 1
void ssObject::readyToPrint1(int _t1)
{
void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 1, _a);
}
// SIGNAL 2
void ssObject::readyToPrint2(float _t1)
{
void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 2, _a);
}
QT_END_MOC_NAMESPACE
Also explain how
QObject::connect(ssobj1,SIGNAL(readyToPrint()),ssobj1,SLOT(print()))
expands
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者更好的是,创建一个像这样的虚拟
QObject.h
文件,然后创建
moc QObject.h
,这就是我得到的:Or even better, create a dummy
QObject.h
file like thisand then
moc QObject.h
, this is what I got:从下面的代码来看,
QObject
的qt_metacall
也是由moc
生成的。从下面的代码(https:/ /github.com/qt/qtbase/blob/dev/src/tools/moc/generator.cpp#LL971C41-L971C50):
ssObject::qt_metacall
和之间的区别QObject::qt_metacall
是对基qt_metacall()
的调用。所以我猜
QObject::qt_metacall
将类似于ssObject::qt_metacall
但不会调用_id = QObject::qt_metacall(_c, _id, _a) ;
。from the code below,
QObject
'sqt_metacall
is also generated bymoc
.From the code below (https://github.com/qt/qtbase/blob/dev/src/tools/moc/generator.cpp#LL971C41-L971C50):
The difference between
ssObject::qt_metacall
andQObject::qt_metacall
is the calling of baseqt_metacall()
.So I guess the
QObject::qt_metacall
will be similar tossObject::qt_metacall
but does not call_id = QObject::qt_metacall(_c, _id, _a);
.