代码:
testApp2.h
- #pragma once
-
- #include
- #include "ui_testApp2.h"
- #include
- #include
- #include
- #include
- 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
(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
(sender())->property("isBoy").toBool()) { - senderPtr = boy;
- }
- else {
- senderPtr = girl;
- }
- senderPtr->setAge(senderPtr->getAge() + 1);
- }
-
- void testApp2::onAgeSpinClicked(int value) {
- qPreson* senderPtr;
- if (qobject_cast
(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
- #include
- 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的;