元对象系统功能

元对象系统功能

建立工程

布局页面





布局页面

修改原件名称

建立元对象





函数作为接口











增加一些固定的属性







#-------------------------------------------------
#
# Project created by QtCreator 2023-10-24T21:54:44
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = sample_3
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        widget.cpp \
    tperson.cpp

HEADERS += \
        widget.h \
    tperson.h

FORMS += \
        widget.ui

#ifndef TPERSON_H
#define TPERSON_H

#include <QObject>

class TPerson : public QObject
{
    Q_OBJECT
    //提示信息
    Q_CLASSINFO("author","wang")
    Q_CLASSINFO("company","UPC")
    Q_CLASSINFO("version","2.0.0")

    Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged)
    Q_PROPERTY(QString name MEMBER m_name)
    Q_PROPERTY(int score MEMBER m_score)
public:
    explicit TPerson(QString name,QObject *parent = nullptr);
    ~TPerson();
    //定义函数接口--对接年龄属性
    int age();//读年龄
    void setAge(quint8 ageValue);//写年龄
    void incAge();//增加年龄

signals:
    void ageChanged(int ageValue);//年龄改变时,触发的函数

public slots:

private:
    //任务属性
    QString m_name;
    int m_age = 10;
    int m_score = 79;
};

#endif // TPERSON_H

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
class TPerson;//定义一个类

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private :
    //定义二个类指针
    TPerson *boy;
    TPerson *girl;
private slots:
    //定义二个槽函数
    void do_ageChanged(int value);
    void do_spinChanged(int argl);

    void on_btnBoylnc_clicked();

    void on_btnGirlnc_clicked();

    void on_btnClear_clicked();

    void on_btnClassinfo_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

#include "tperson.h"

TPerson::TPerson(QString name,QObject *parent) : QObject(parent),m_name(name)
{

}

TPerson::~TPerson()
{
    qDebug("TPerson类的对象被删除");

}

int TPerson::age()
{
    return m_age;
}

void TPerson::setAge(quint8 ageValue)
{
    if(m_age != ageValue)
    {
        m_age = ageValue;
        emit ageChanged(m_age);
    }

}

void TPerson::incAge()
{
    ++m_age;
    emit ageChanged(m_age);
}

#include "widget.h"
#include "ui_widget.h"
#include "tperson.h"
#include<QMetaProperty>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //初始化界面
    boy = new TPerson("小明",this);
    boy->setProperty("sex","boy");
    boy->setProperty("age",10);
    boy->setProperty("score",70);
    girl = new TPerson("小丽",this);
    girl->setProperty("sex","gril");
    girl->setAge(20);
    ui->spinBoy->setProperty("isBoy",true);
    ui->spinGirl->setProperty("isBoy",false);
    connect(boy,SIGNAL(ageChanged(int)),ui->spinBoy,SLOT(setValue(int)));
    connect(girl,SIGNAL(ageChanged(int)),ui->spinGirl,SLOT(setValue(int)));
    connect(boy,SIGNAL(ageChanged(int)),this,SLOT(do_ageChanged(int)));
    connect(girl,SIGNAL(ageChanged(int)),this,SLOT(do_ageChanged(int)));
    connect(ui->spinBoy,SIGNAL(valueChanged(int)),this,SLOT(do_spinChanged(int)));
    connect(ui->spinGirl,SIGNAL(valueChanged(int)),this,SLOT(do_spinChanged(int)));
}

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

void Widget::do_ageChanged(int value)
{
    //Q_UNUSED(value);
    TPerson *person = qobject_cast<TPerson*>(sender());
    QString str = QString("name=%1,sex=%2,年龄=%3")
            .arg(person->property("name").toString())
            .arg(person->property("sex").toString())
            .arg(value);
    ui->plainTextEdit->appendPlainText(str);
}

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


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

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

void Widget::on_btnClear_clicked()
{
    ui->plainTextEdit->clear();
}

void Widget::on_btnClassinfo_clicked()
{
    const QMetaObject *meta = boy->metaObject();
    ui->plainTextEdit->appendPlainText(QString("类名称:%1\n").arg(meta->className()));

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

    ui->plainTextEdit->appendPlainText("\n类信息(classInfo):");
    for(int i = meta->propertyOffset();i < meta->propertyCount();i++)
    {
        QMetaClassInfo classInfo = meta->classInfo(i);
        ui->plainTextEdit->appendPlainText(
                    QString("Name=%1,value=%2").arg(classInfo.name()).arg(classInfo.value()));
    }
}
相关推荐
鹏大师运维3 小时前
【功能介绍】信创终端系统上各WPS版本的授权差异
linux·wps·授权·麒麟·国产操作系统·1024程序员节·统信uos
亦枫Leonlew4 小时前
微积分复习笔记 Calculus Volume 1 - 4.7 Applied Optimization Problems
笔记·数学·微积分·1024程序员节
小肥象不是小飞象4 小时前
(六千字心得笔记)零基础C语言入门第八课——函数(上)
c语言·开发语言·笔记·1024程序员节
一个通信老学姐13 小时前
专业130+总400+武汉理工大学855信号与系统考研经验电子信息与通信工程,真题,大纲,参考书。
考研·信息与通信·信号处理·1024程序员节
力姆泰克13 小时前
看电动缸是如何提高农机的自动化水平
大数据·运维·服务器·数据库·人工智能·自动化·1024程序员节
力姆泰克13 小时前
力姆泰克电动缸助力农业机械装备,提高农机的自动化水平
大数据·服务器·数据库·人工智能·1024程序员节
程思扬14 小时前
为什么Uptime+Kuma本地部署与远程使用是网站监控新选择?
linux·服务器·网络·经验分享·后端·网络协议·1024程序员节
转世成为计算机大神14 小时前
网关 Spring Cloud Gateway
java·网络·spring boot·1024程序员节
paopaokaka_luck14 小时前
基于Spring Boot+Vue的助农销售平台(协同过滤算法、限流算法、支付宝沙盒支付、实时聊天、图形化分析)
java·spring boot·小程序·毕业设计·mybatis·1024程序员节
幼儿园园霸柒柒15 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节