界面:

ButterflyGraph:

可以看出,本工程在主程序main里调用窗口界面qmywidget,窗口界面继承了QWidget,并调用自定义类qperson,推测是qmywidget类中的一个属性

qmywidget的实现,调用了显示界面ui_qmywidget,包含了元对象QMetaProperty

UML图:

QmyWidget中,on开头的都是槽函数,QmyWidget是主窗口,所以不发射信号,只接收信号

main.cpp

 1 #include "qmywidget.h"
2 #include <QApplication>
3
4 int main(int argc, char *argv[])
5 {
6 QApplication a(argc, argv);
7 QmyWidget w;
8 w.show();
9
10 return a.exec();
11 }

qperson.h

 1 #ifndef QPERSON_H
2 #define QPERSON_H
3
4 #include <QObject>
5
6 class QPerson : public QObject
7 {
8 Q_OBJECT
9
10 Q_CLASSINFO("author","Wang")
11 Q_CLASSINFO("company","UPC")
12 Q_CLASSINFO("version","1.0.0")
13
14 Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged)
15 Q_PROPERTY(QString name MEMBER m_name)
16 Q_PROPERTY(int score MEMBER m_score)
17 private:
18 int m_age=10;
19 QString m_name;
20 int m_score=79;
21 public:
22 explicit QPerson(QString fName, QObject *parent = nullptr);
23
24 int age();
25 void setAge(int value);
26
27 void incAge();
28 signals:
29 void ageChanged( int value);
30
31 public slots:
32 };
33
34 #endif // QPERSON_H

qmywidget.cpp

  1 #include "qmywidget.h"
2 #include "ui_qmywidget.h"
3 #include <QMetaProperty>
4
5 QmyWidget::QmyWidget(QWidget *parent) :
6 QWidget(parent),
7 ui(new Ui::QmyWidget)
8 {//构造函数
9 ui->setupUi(this);
10
11 boy=new QPerson("王小明");
12 boy->setProperty("score",95);
13 boy->setProperty("age",10);
14 boy->setProperty("sex","Boy");//动态属性
15 // connect(boy,SIGNAL(ageChanged(int)),this,SLOT(on_ageChanged(int)));
16 connect(boy,&QPerson::ageChanged,this,&QmyWidget::on_ageChanged);
17
18 girl=new QPerson("张小丽");
19 girl->setProperty("score",81);
20 girl->setProperty("age",20);
21 girl->setProperty("sex","Girl");//动态属性
22 connect(girl,&QPerson::ageChanged,this,&QmyWidget::on_ageChanged);
23
24 ui->spinBoy->setProperty("isBoy",true); //动态属性
25 ui->spinGirl->setProperty("isBoy",false);
26
27 // 不能使用此形式,因为QSpinBox有两种参数形式的valueChanged()信号
28 // connect(ui->spinGirl,&QSpinBox::valueChanged,
29 // this,&QmyWidget::on_spinBoy_valueChanged);
30 connect(ui->spinGirl,SIGNAL(valueChanged(int)),
31 this,SLOT(on_spin_valueChanged(int)));
32 connect(ui->spinBoy,SIGNAL(valueChanged(int)),
33 this,SLOT(on_spin_valueChanged(int)));
34 }
35
36 QmyWidget::~QmyWidget()
37 {
38 delete ui;
39 }
40
41 void QmyWidget::on_ageChanged( int value)
42 {//响应QPerson的ageChanged()信号
43 Q_UNUSED(value);
44 QPerson *aPerson = qobject_cast<QPerson *>(sender()); //类型投射
45 QString hisName=aPerson->property("name").toString(); //姓名
46 // QString hisName=aPerson->name(); //获取姓名,错误
47 QString hisSex=aPerson->property("sex").toString(); //动态属性
48 int hisAge=aPerson->age();//通过接口函数获取年龄
49 // int hisAge=aPerson->property("age").toInt();//通过属性获得年龄
50
51 ui->textEdit->appendPlainText(hisName+","+hisSex
52 +QString::asprintf(",年龄=%d",hisAge));
53 }
54
55 void QmyWidget::on_btnClear_clicked()
56 {//"清空文本框"按钮
57 ui->textEdit->clear();
58 }
59
60 void QmyWidget::on_btnBoyInc_clicked()
61 {//"boy长大一岁"按钮
62 boy->incAge();
63 }
64
65 void QmyWidget::on_btnGirlInc_clicked()
66 {//"girl长大一岁"按钮
67 girl->incAge();
68 }
69
70 void QmyWidget::on_spin_valueChanged(int arg1)
71 {//响应界面上spinBox的valueChanged(int)信号
72 Q_UNUSED(arg1);
73 QSpinBox *spinBox = qobject_cast<QSpinBox *>(sender());
74 if (spinBox->property("isBoy").toBool())
75 boy->setAge(spinBox->value());
76 else
77 girl->setAge(spinBox->value());
78 }
79
80 void QmyWidget::on_btnClassInfo_clicked()
81 {//"类的元对象信息"按钮
82 const QMetaObject *meta=boy->metaObject();
83 // const QMetaObject *meta=girl->metaObject();
84 // const QMetaObject *meta=ui->spinBoy->metaObject();
85 ui->textEdit->clear();
86
87 ui->textEdit->appendPlainText("==元对象信息==\n");
88 ui->textEdit->appendPlainText(QString("类名称:%1\n").arg(meta->className()));
89
90 ui->textEdit->appendPlainText("property");
91 for (int i=meta->propertyOffset();i<meta->propertyCount();i++)
92 {
93 const char* propName=meta->property(i).name();
94 ui->textEdit->appendPlainText(
95 QString("属性名称=%1,属性值=%2").arg(propName).arg(boy->property(propName).toString()));
96 }
97
98 ui->textEdit->appendPlainText("");
99 ui->textEdit->appendPlainText("classInfo");
100 for (int i=meta->classInfoOffset();i<meta->classInfoCount();++i)
101 {
102 QMetaClassInfo classInfo=meta->classInfo(i);
103 ui->textEdit->appendPlainText(
104 QString("Name=%1; Value=%2").arg(classInfo.name()).arg(classInfo.value()));
105 }
106
107 }

qperson.cpp

 1 #include "qperson.h"
2
3 QPerson::QPerson(QString fName,QObject *parent) : QObject(parent)
4 { //构造函数
5 m_name=fName;
6 }
7
8 int QPerson::age()
9 { //返回age
10 return m_age;
11 }
12
13 void QPerson::setAge(int value)
14 {//设置age
15 m_age=value;
16 emit ageChanged(m_age); //发射信号
17 }
18
19 void QPerson::incAge()
20 {
21 m_age++;
22 emit ageChanged(m_age);//发射信号
23 }

最新文章

  1. Javascript之旅——第五站:说说那些所谓的包装类型
  2. Java之组合数组2
  3. Android SDK Manager 更新代理配置 ,蛋碎了
  4. Block之变量作用域
  5. Cocos2D-X v3.0 alpha1环境搭建
  6. LoadRuner性能测试之内存分析方法及步骤(Windows)
  7. css样式中如何设置中文字体?
  8. Python: 内置私有方法
  9. Spark SQL自定义外部数据源
  10. 装饰器,栈 ,asyncio 代码
  11. 图解项目管理流程:禅道&amp;JIRA中的操作
  12. 潭州课堂25班:Ph201805201 爬虫基础 第十课 图像处理- 极验验证码 (课堂笔记)
  13. [原创]H5前端性能测试工具介绍
  14. ERROR: please install the following Perl modules before executing ./mysql_install_db
  15. ReentrantLock 和 Condition的使用
  16. Mysql全文搜索match against的用法
  17. MySql 8 命令
  18. 查找内容grep命令
  19. 《[MySQL技术内幕:SQL编程》读书笔记
  20. mysqlcppconn之ConnectOptionsMap的使用

热门文章

  1. redis常用数据类型对应的数据结构
  2. java.net.BindException: Problem binding to [hadoop103:8031] java.net.BindException
  3. 010_Nginx入门
  4. KubeEdge EdgeMesh设计原理
  5. Java执行groovy脚本的两种方式
  6. Go-06-数据类型、常量、运算符
  7. (九)Docker-PS 详解
  8. 18. VUE created 方法作用
  9. (数据科学学习手札118)Python+Dash快速web应用开发——特殊部件篇
  10. math random模块