QMetaObject编译器解释一下?

发布于 2024-08-19 17:53:01 字数 3633 浏览 3 评论 0原文

我想知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙气飘飘 2024-08-26 17:53:01

或者更好的是,创建一个像这样的虚拟 QObject.h 文件

class QObject
{
    Q_OBJECT
};

,然后创建 moc QObject.h,这就是我得到的:

/****************************************************************************
** Meta object code from reading C++ file 'QObject.h'
**
** Created by: The Qt Meta Object Compiler version 67 (Qt 5.15.2)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

#include <memory>
#include ".\template\QObject.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'QObject.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 67
#error "This file was generated using the moc from 5.15.2. 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
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
struct qt_meta_stringdata_QObject_t {
    QByteArrayData data[1];
    char stringdata0[8];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
    Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
    qptrdiff(offsetof(qt_meta_stringdata_QObject_t, stringdata0) + ofs \
        - idx * sizeof(QByteArrayData)) \
    )
static const qt_meta_stringdata_QObject_t qt_meta_stringdata_QObject = {
    {
QT_MOC_LITERAL(0, 0, 7) // "QObject"

    },
    "QObject"
};
#undef QT_MOC_LITERAL

static const uint qt_meta_data_QObject[] = {

 // content:
       8,       // revision
       0,       // classname
       0,    0, // classinfo
       0,    0, // methods
       0,    0, // properties
       0,    0, // enums/sets
       0,    0, // constructors
       0,       // flags
       0,       // signalCount

       0        // eod
};

void QObject::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
    Q_UNUSED(_o);
    Q_UNUSED(_id);
    Q_UNUSED(_c);
    Q_UNUSED(_a);
}

QT_INIT_METAOBJECT const QMetaObject QObject::staticMetaObject = { {
    nullptr,
    qt_meta_stringdata_QObject.data,
    qt_meta_data_QObject,
    qt_static_metacall,
    nullptr,
    nullptr
} };


const QMetaObject *QObject::metaObject() const
{
    return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}

void *QObject::qt_metacast(const char *_clname)
{
    if (!_clname) return nullptr;
    if (!strcmp(_clname, qt_meta_stringdata_QObject.stringdata0))
        return static_cast<void*>(this);
    return nullptr;
}

int QObject::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    return _id;
}
QT_WARNING_POP
QT_END_MOC_NAMESPACE

Or even better, create a dummy QObject.h file like this

class QObject
{
    Q_OBJECT
};

and then moc QObject.h, this is what I got:

/****************************************************************************
** Meta object code from reading C++ file 'QObject.h'
**
** Created by: The Qt Meta Object Compiler version 67 (Qt 5.15.2)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

#include <memory>
#include ".\template\QObject.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'QObject.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 67
#error "This file was generated using the moc from 5.15.2. 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
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
struct qt_meta_stringdata_QObject_t {
    QByteArrayData data[1];
    char stringdata0[8];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
    Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
    qptrdiff(offsetof(qt_meta_stringdata_QObject_t, stringdata0) + ofs \
        - idx * sizeof(QByteArrayData)) \
    )
static const qt_meta_stringdata_QObject_t qt_meta_stringdata_QObject = {
    {
QT_MOC_LITERAL(0, 0, 7) // "QObject"

    },
    "QObject"
};
#undef QT_MOC_LITERAL

static const uint qt_meta_data_QObject[] = {

 // content:
       8,       // revision
       0,       // classname
       0,    0, // classinfo
       0,    0, // methods
       0,    0, // properties
       0,    0, // enums/sets
       0,    0, // constructors
       0,       // flags
       0,       // signalCount

       0        // eod
};

void QObject::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
    Q_UNUSED(_o);
    Q_UNUSED(_id);
    Q_UNUSED(_c);
    Q_UNUSED(_a);
}

QT_INIT_METAOBJECT const QMetaObject QObject::staticMetaObject = { {
    nullptr,
    qt_meta_stringdata_QObject.data,
    qt_meta_data_QObject,
    qt_static_metacall,
    nullptr,
    nullptr
} };


const QMetaObject *QObject::metaObject() const
{
    return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}

void *QObject::qt_metacast(const char *_clname)
{
    if (!_clname) return nullptr;
    if (!strcmp(_clname, qt_meta_stringdata_QObject.stringdata0))
        return static_cast<void*>(this);
    return nullptr;
}

int QObject::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    return _id;
}
QT_WARNING_POP
QT_END_MOC_NAMESPACE
蓝眼睛不忧郁 2024-08-26 17:53:01

从下面的代码来看,QObjectqt_metacall也是由moc生成的。

class Q_CORE_EXPORT QObject
{
    Q_OBJECT
}

从下面的代码(https:/ /github.com/qt/qtbase/blob/dev/src/tools/moc/generator.cpp#LL971C41-L971C50):

if (!purestSuperClass.isEmpty() && !isQObject) {
  QByteArray superClass = purestSuperClass;
  fprintf(out, "    _id = %s::qt_metacall(_c, _id, _a);\n", superClass.constData());
}

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's qt_metacall is also generated by moc.

class Q_CORE_EXPORT QObject
{
    Q_OBJECT
}

From the code below (https://github.com/qt/qtbase/blob/dev/src/tools/moc/generator.cpp#LL971C41-L971C50):

if (!purestSuperClass.isEmpty() && !isQObject) {
  QByteArray superClass = purestSuperClass;
  fprintf(out, "    _id = %s::qt_metacall(_c, _id, _a);\n", superClass.constData());
}

The difference between ssObject::qt_metacall and QObject::qt_metacall is the calling of base qt_metacall().

So I guess the QObject::qt_metacall will be similar to ssObject::qt_metacall but does not call _id = QObject::qt_metacall(_c, _id, _a);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文