首页 > 其他 > 详细

Qt moc元对象编译器的原理和场景(反射)

时间:2020-12-15 18:01:10      阅读:37      评论:0      收藏:0      [点我收藏+]

Qt Meta-Object Compiler (moc)

元对象编译器,能够实现运行时类型识别。可以用在需要用到反射机制的情况下,qml使用c++的类和方法就是使用了这个特性,通过Q_PROPERTY在类内注册属性;

myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QObject>

class MyClass : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(int size READ size WRITE setSize NOTIFY sizeChanged)
public:
    explicit MyClass(QObject *parent = nullptr);

    Q_INVOKABLE  void PrintSelf();
    QString name() const
    {
        return m_name;
    }

    size_t size() const
    {
        return m_size;
    }

signals:

    void nameChanged(QString name);

    void sizeChanged(size_t size);

public slots:

void setName(QString name)
{
    if (m_name == name)
        return;

    m_name = name;
    emit nameChanged(m_name);
}

void setSize(size_t size)
{
    if (m_size == size)
        return;

    m_size = size;
    emit sizeChanged(m_size);
}

private:
    QString m_name;
    int m_size;
};

#endif // MYCLASS_H

main.cpp

#include <QCoreApplication>
#include <iostream>
#include <QVariant>
#include <QDebug>
#include <QMetaMethod>
#include "myclass.h"
using namespace std;

void test()
{
    cout << "-------test property-----------" << "\n";
    MyClass newObj;
    newObj.setProperty("type", "fuck");
    newObj.setProperty("Member1", 66);
    cout << newObj.property( "Member1" ).toString().toStdString() << "\n";
    const QMetaObject* mobj = newObj.metaObject();
    qDebug() << "obj name = " << mobj->className();

    int propertyCount = mobj->propertyCount();
    for(int n = 0; n < propertyCount; ++n)
    {
        qDebug() << "property index : " << n;
        QMetaProperty property = mobj->property(n);
        qDebug() << "property : " << property.name();
        qDebug() << "property value : " << property.read(&newObj);
    }

    int methodCount = mobj->methodCount();
    for(int n = 0; n < methodCount; ++n)
    {
        qDebug() << "method index : " << n;
        QMetaMethod method = mobj->method(n);
        qDebug() << "method name : " << method.name();
        method.invoke(&newObj);
    }

 QMetaObject::invokeMethod( &newObj, "PrintSelf", Qt::DirectConnection);

    MyClass* myClass = dynamic_cast<MyClass*>(mobj->newInstance());
    cout << "--------end----------" << "\n";
}

int main(int argc, char *argv[])
{
    test();
    QCoreApplication a(argc, argv);

    return a.exec();
}

Qt moc元对象编译器的原理和场景(反射)

原文:https://www.cnblogs.com/wzxNote/p/14139537.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!