Qt学习笔记:Demo1[练习属性和信号和槽机制]

代码:

testApp2.h

#pragma once

#include <QtWidgets/QWidget>
#include "ui_testApp2.h"
#include<qPreson.h>
#include<qdebug.h>
#include<qobject.h>
#include<qmetaobject.h>
class testApp2 : public QWidget
{
    Q_OBJECT

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

private:
	void init();
	void initConnect();
public slots:
	void showClassInf();
	void onAgeIncClicked() ;
	void onAgeSpinClicked(int value);
	void onClearBntClicked();
	void onAgeChange(int value);
private:
    Ui::testApp2Class ui;
	qPreson* boy;
	qPreson* girl;
};

testAPP2.cpp

#include "testApp2.h"

testApp2::testApp2(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
	init();
	initConnect();
}

testApp2::~testApp2()
{}

void testApp2::init()
{
	boy = new qPreson("lhm");
	girl = new qPreson("dq");
	ui.spinBoxBoyAge->setProperty("isBoy", true);
	ui.spinBoxgirlAge->setProperty("isBoy", false);
	ui.boyAgeInc->setProperty("isBoy", true);
	ui.girlAgeInc->setProperty("isBoy", false);
	boy->setProperty("age", 18);
	girl->setProperty("age", 17);
	boy->setProperty("score", 80);
	girl->setProperty("score", 79);
}
void testApp2::initConnect() {
	QObject::connect(ui.clear, &QPushButton::clicked, this, &testApp2::onClearBntClicked);
	QObject::connect(boy, &qPreson::ageChanged, this, &testApp2::onAgeChange);
	QObject::connect(girl, &qPreson::ageChanged, this, &testApp2::onAgeChange);
	QObject::connect(ui.spinBoxBoyAge,static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),this, &testApp2::onAgeSpinClicked);
	QObject::connect(ui.spinBoxgirlAge, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),this, &testApp2::onAgeSpinClicked);
	QObject::connect(ui.boyAgeInc, &QPushButton::clicked, this, &testApp2::onAgeIncClicked);
	QObject::connect(ui.girlAgeInc, &QPushButton::clicked, this, &testApp2::onAgeIncClicked);
	QObject::connect(ui.showClassInf, &QPushButton::clicked, this, &testApp2::showClassInf);
}


void testApp2::onClearBntClicked() {
	ui.textEdit->clear();
}

void testApp2::onAgeChange(int x) {
	QObject* senderPtr = qobject_cast<QObject*>(sender());
	QString information = senderPtr->property("name").toString()+" age :"+senderPtr->property("age").toString()+"\n";
	ui.textEdit->append(information);
}

void testApp2::onAgeIncClicked() {
	qPreson* senderPtr;
	if (qobject_cast<QPushButton*>(sender())->property("isBoy").toBool()) {
		senderPtr = boy;
	}
	else {
		senderPtr = girl;
	}
	senderPtr->setAge(senderPtr->getAge() + 1);
}

void testApp2::onAgeSpinClicked(int value) {
	qPreson* senderPtr;
	if (qobject_cast<QSpinBox*>(sender())->property("isBoy").toBool()) {
		senderPtr = boy;
	}
	else {
		senderPtr = girl;
	}
	senderPtr->setAge(value);
}

void testApp2::showClassInf() {
	const QMetaObject* meta = girl->metaObject();
	QString inf;
	inf.append("----MeatObjectInformation-----\n");
	inf.append(QString("typename: %1\n").arg(meta->className()));
	for (int rcy = 1; rcy <= meta->classInfoCount()+1; rcy++) {
		QMetaProperty prop = meta->property(rcy);
		const char* propName = prop.name();
		QString propValue = girl->property(propName).toString();
		inf.append(QString("property name: %1 - property value %2\n").arg(propName, propValue));
	}
	ui.textEdit->append(inf);
}

qPreson.h:

#pragma once
#include<qobject.h>
#include<qdebug.h>
class qPreson :public QObject{
		Q_OBJECT
		Q_CLASSINFO("author", "LHM")
		Q_CLASSINFO("data", "2022/11/27")
		Q_PROPERTY(unsigned age READ getAge WRITE setAge NOTIFY ageChanged)
		Q_PROPERTY(QString name MEMBER name)
		Q_PROPERTY(int score MEMBER score)
private:
	QString name;
	int age = 0, score = 0;
public:

	explicit qPreson(const QString& name) {
		this->name = name;
		qDebug() << this->name;
	}
	void setAge(unsigned value) {
		this->age = value;
		emit ageChanged(value);
	}
	unsigned getAge() {
		return age;
	}
signals:
	void ageChanged(int);
};

收获总结:

-:对属性的书写更加熟悉

-:熟悉了QObject::connect的用法

-:初步了解设计信号与槽之间的关系[其实和写非qt项目没什么大差别,主要就qt有一些强大的魔改根据让你使用]

        ·: 这边我设置了一个property为age,当我们setage的时候,会处罚一个信号,这个信号我设置与testApp的一个onAgeChange相连接,其会在text上显示最新的消息,这样的好处是将改变Age的代码与接受信号的代码相隔离,减少了耦合;

-:还有一个就是showClassInformat里面的循环部分,其中QMateObject并不是继承自QObject的,我们要调用其mateobject函数还获取,换句话说,QObject是持有QMateObject的;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值