1)创建类Person,成员变量,构造函数,自定义信号
2)实现person的一些函数
3)在widget中添加person类型的成员变量
4)初始化,连接信号和槽
5)槽函数1
6)槽函数2 自定义的槽函数,需要通过connect连接信号ageChanged和槽函数on_ageChanged
7)槽函数3 输出元对象信息,包括属性键值对和QClassInfo键值对
源代码链接:https://download.csdn.net/download/weixin_44500344/86543158
1)qperson.h文件
#ifndef QPERSON_H
#define QPERSON_H
#include
class QPerson : public QObject
{
Q_OBJECT
Q_CLASSINFO("author","cuiqianqian")
Q_CLASSINFO("company","university")
Q_PROPERTY(unsigned age READ age WRITE setAge NOTIFY ageChanged)
Q_PROPERTY(QString name MEMBER m_name)
Q_PROPERTY(int score MEMBER m_score)
private:
unsigned m_age;
QString m_name;
int m_score;
public:
explicit QPerson(QString name,unsigned age,int score,QObject *parent = nullptr);
unsigned age();
void setAge(unsigned value);
void incAge();
signals:
void ageChanged(unsigned value);
public slots:
};
#endif // QPERSON_H
2)qperson.cpp文件
#include "qperson.h"
QPerson::QPerson(QString name,unsigned age,int score,QObject *parent) : QObject(parent)
{
m_name=name;
m_age=age;
m_score=score;
}
unsigned QPerson::age()
{
return m_age;
}
void QPerson::setAge(unsigned value)
{
m_age=value;
emit(ageChanged(m_age));
}
void QPerson::incAge()
{
m_age++;
}
3)widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include "qperson.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_clearButton_clicked();
void on_boyButton_clicked();
void on_girlButton_clicked();
void on_boyBox_valueChanged(int arg1);
void on_girlBox_valueChanged(int arg1);
void on_classInfoButton_clicked();
//自定义槽函数
void on_ageChanged();
private:
Ui::Widget *ui;
QPerson *boy;
QPerson *girl;
};
#endif // WIDGET_H
4)widget.cpp文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//初始化
boy = new QPerson("张三",11,90);
girl= new QPerson("小红",21,100);
ui->boyBox->setValue(boy->age());
ui->girlBox->setValue(girl->age());
//当调用person->setAge(),会发出ageChanged函数,执行on_ageChanged()槽函数,输出成员信息
connect(boy,&QPerson::ageChanged,this,&Widget::on_ageChanged);
connect(girl,&QPerson::ageChanged,this,&Widget::on_ageChanged);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_clearButton_clicked()
{
ui->plainTextEdit->clear();
}
//男孩长一岁
void Widget::on_boyButton_clicked()
{
boy->incAge();
ui->boyBox->setValue(boy->age());
}
//女孩长一岁
void Widget::on_girlButton_clicked()
{
girl->incAge();
ui->girlBox->setValue(girl->age());
}
void Widget::on_boyBox_valueChanged(int arg1)
{
boy->setAge(arg1);
}
void Widget::on_girlBox_valueChanged(int arg1)
{
girl->setAge(arg1);
}
//自定义的槽函数
void Widget::on_ageChanged()
{
//显示成员信息
QPerson *aPerson=qobject_cast<QPerson *>(sender());
QString aName=aPerson->property("name").toString();
unsigned aAge=aPerson->age();
int aSorce=aPerson->property("score").toInt();
ui->plainTextEdit->appendPlainText(aName+
QString::asprintf(",年龄=%d,成绩=%d",aAge,aSorce));
}
void Widget::on_classInfoButton_clicked()
{
const QMetaObject *meta=boy->metaObject();
ui->plainTextEdit->clear();
ui->plainTextEdit->appendPlainText("==元对象信息==");
ui->plainTextEdit->appendPlainText(
QString("类命名:%1\n").arg(meta->className()));
ui->plainTextEdit->appendPlainText("property");
for(int i=meta->propertyOffset();i<meta->propertyCount();i++){
QMetaProperty prop=meta->property(i);
const char *propName=prop.name();
QString propValue=boy->property(propName).toString();
ui->plainTextEdit->appendPlainText(
QString("属性名称:%1,属性值:%2").arg(propName).arg(propValue));
}
ui->plainTextEdit->appendPlainText("");
ui->plainTextEdit->appendPlainText("classInfo");
for(int i=meta->classInfoOffset();i<meta->classInfoCount();i++){
QMetaClassInfo classInfo=meta->classInfo(i);
ui->plainTextEdit->appendPlainText(
QString("Name=%1,Value=%2").arg(classInfo.name()).arg(classInfo.value()));
}
}