Qt反射初探

4 篇文章 0 订阅
// reflexdemo.h
#ifndef REFLEXDEMO_H
#define REFLEXDEMO_H

#include <QObject>

class ReflexDemo : public QObject {
  Q_OBJECT
public:
  explicit ReflexDemo(QObject *parent = nullptr);
  Q_INVOKABLE void public_method(int a);
  void no_q_invokable_public_method(int a);
  Q_PROPERTY(int public_field MEMBER p_field)
  int p_field;

protected:
  Q_INVOKABLE void protected_method(int a);
  void no_q_invokable_protected_method(int a);
  Q_PROPERTY(int protected_field)
signals:
  void demo_signal();
protected slots:
  void demo_slot();

private:
  Q_INVOKABLE void private_method(int a);
  void no_q_invokable_private_method(int a);
  Q_PROPERTY(int private_field)
};

#endif // REFLEXDEMO_H

// main.cc
#include "reflexdemo.h"
#include <QCoreApplication>
#include <QDebug>
#include <QMetaObject>
#include <QMetaProperty>

int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);
  ReflexDemo rd;
  auto meta_obj = rd.metaObject();
  qInfo() << "class name: " << meta_obj->className();
  auto property_count = meta_obj->propertyCount();
  qInfo() << "property_count: " << property_count;
  for (int i = 0; i < property_count; ++i) {
    auto property = meta_obj->property(i);
    qInfo() << "property[" << i << "] is " << property.name();
  }
  auto method_count = meta_obj->methodCount();
  qInfo() << "method_count: " << method_count;
  for (int i = 0; i < method_count; ++i) {
    auto method = meta_obj->method(i);
    qInfo() << "method[" << i << "] is " << method.name();
  }

  // rd.public_field
  // rd.protected_field
  // rd.private_field
  rd.setProperty("public_field", 100);
  auto value = rd.property("public_field").toInt();
  qInfo() << "public_field value:" << value;

  rd.setProperty("protected_field", 33);
  value = rd.property("protected_field").toInt();
  qInfo() << "protected_field value:" << value;

  return a.exec();
}

输出

class name:  ReflexDemo
property_count:  4
property[ 0 ] is  objectName
property[ 1 ] is  public_field
property[ 2 ] is  protected_field
property[ 3 ] is  private_field
method_count:  10
method[ 0 ] is  "destroyed"
method[ 1 ] is  "destroyed"
method[ 2 ] is  "objectNameChanged"
method[ 3 ] is  "deleteLater"
method[ 4 ] is  "_q_reregisterTimers"
method[ 5 ] is  "demo_signal"
method[ 6 ] is  "demo_slot"
method[ 7 ] is  "public_method"
method[ 8 ] is  "protected_method"
method[ 9 ] is  "private_method"
public_field value: 100
ReflexDemo::setProperty: Property "protected_field" invalid, read-only or does not exist
ReflexDemo::property: Property "protected_field" invalid or does not exist
protected_field value: 0
  • 结论
    类的成员要加入到qt的反射机制中,必须继承QObject ,在方法前加 Q_INVOKABLE,
    qt的property不等类的成员变量
 Q_PROPERTY(type name
             (READ getFunction [WRITE setFunction] |
              MEMBER memberName [(READ getFunction | WRITE setFunction)])
             [RESET resetFunction]
             [NOTIFY notifySignal]
             [REVISION int]
             [DESIGNABLE bool]
             [SCRIPTABLE bool]
             [STORED bool]
             [USER bool]
             [CONSTANT]
             [FINAL])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tadus_zeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值