其他分享
首页 > 其他分享> > 元对象测试

元对象测试

作者:互联网

界面

image

代码

qperson.h

#ifndef QPERSON_H
#define QPERSON_H

#include <QObject>

class QPerson : public QObject
{
    Q_OBJECT
    Q_CLASSINFO("author", "Shao ZHeming")
    Q_CLASSINFO("company", "nio")
    Q_CLASSINFO("version", "1.0")
    Q_PROPERTY(unsigned age READ getAge WRITE setAge NOTIFY ageChanged)
    Q_PROPERTY(QString name MEMBER m_name) //和属性绑定
    Q_PROPERTY(int score MEMBER m_score)

private:
    unsigned m_age = 10;
    QString m_name;
    int m_score = 79;

public:
    explicit QPerson(QString name, QObject *parent = nullptr);

    unsigned getAge(void);
    void setAge(unsigned value);
    void incAge(void);

signals:
    unsigned ageChanged(unsigned value);
};

#endif // QPERSON_H

qperson.cpp

#include "qperson.h"

QPerson::QPerson(QString name, QObject *parent) : QObject(parent)
{
    m_name = name;
}

unsigned QPerson::getAge()
{
    return m_age;
}

void QPerson::setAge(unsigned value)
{
    m_age = value;
    emit(ageChanged(m_age)); //向函数发射信号
}

void QPerson::incAge()
{
    m_age++;
    emit(ageChanged(m_age)); //向函数发射信号
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "qperson.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
private:
    QPerson *boy;
    QPerson *girl;

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
    void on_ageChanged(unsigned value);

private slots:
    void on_spin_valueChanged(int arg1);
    void on_btnBoyInc_clicked();
    void on_btnGrilInc_clicked();
    void on_btnClassInfo_clicked();
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QMetaProperty>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->spinBoxBoy->setProperty("isBoy", true);
    ui->spinBoxGirl->setProperty("isBoy", false);

    boy = new QPerson("王小明");
    boy->setProperty("score", 95);
    boy->setProperty("age", 10);
    boy->setProperty("sex", "boy");
    connect(boy, &QPerson::ageChanged, this, &Widget::on_ageChanged);

    girl = new QPerson("小红");
    girl->setProperty("score", 95);
    girl->setProperty("age", 5);
    girl->setProperty("sex", "girl");
    connect(girl, &QPerson::ageChanged, this, &Widget::on_ageChanged);

    connect(ui->spinBoxBoy, SIGNAL(valueChanged(int)),
            this, SLOT(on_spin_valueChanged(int)));
    connect(ui->spinBoxGirl, SIGNAL(valueChanged(int)),
            this, SLOT(on_spin_valueChanged(int)));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_spin_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    QSpinBox *spinBox = qobject_cast<QSpinBox *>(sender());
    if(spinBox->property("isBoy").toBool())
        boy->setAge(spinBox->value());
    else
        girl->setAge(spinBox->value());
}

void Widget::on_ageChanged(unsigned value)
{
    Q_UNUSED(value);
    QPerson *aPerson = qobject_cast<QPerson *>(sender());
    QString aName = aPerson->property("name").toString();
    QString aSex = aPerson->property("sex").toString();

    unsigned aAge = aPerson->getAge();
    if(aSex == "boy"){
        ui->spinBoxBoy->setValue(aAge);
    }
    else
        ui->spinBoxGirl->setValue(aAge);

    ui->textEdit->appendPlainText(aName + "," + aSex
                                  + QString::asprintf(",年龄=%d", aAge));
}

void Widget::on_btnBoyInc_clicked()
{
    boy->incAge();
}

void Widget::on_btnGrilInc_clicked()
{
    girl->incAge();
}

void Widget::on_btnClassInfo_clicked()
{
//"类的元对象信息"按钮
//    const QMetaObject *meta=boy->metaObject();
    const QMetaObject *meta=girl->metaObject();
//    const QMetaObject *meta=ui->spinBoy->metaObject();
    ui->textEdit->clear();

    ui->textEdit->appendPlainText("==元对象信息==\n");
    ui->textEdit->appendPlainText(QString("类名称:%1\n").arg(meta->className()));

    ui->textEdit->appendPlainText("property");
    for (int i=meta->propertyOffset();i<meta->propertyCount();i++)
    {
        const char* propName=meta->property(i).name();
        ui->textEdit->appendPlainText(
           QString("属性名称=%1,属性值=%2").arg(propName).arg(boy->property(propName).toString()));
    }

    ui->textEdit->appendPlainText("");
    ui->textEdit->appendPlainText("classInfo");
    for (int i=meta->classInfoOffset();i<meta->classInfoCount();++i)
    {
       QMetaClassInfo classInfo=meta->classInfo(i);
        ui->textEdit->appendPlainText(
           QString("Name=%1; Value=%2").arg(classInfo.name()).arg(classInfo.value()));
    }
}


标签:boy,Widget,对象,void,unsigned,ui,测试,QPerson
来源: https://www.cnblogs.com/yoshinb/p/16326038.html