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


    代码:

    testApp2.h

    1. #pragma once
    2. #include
    3. #include "ui_testApp2.h"
    4. #include
    5. #include
    6. #include
    7. #include
    8. class testApp2 : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. testApp2(QWidget *parent = nullptr);
    13. ~testApp2();
    14. private:
    15. void init();
    16. void initConnect();
    17. public slots:
    18. void showClassInf();
    19. void onAgeIncClicked() ;
    20. void onAgeSpinClicked(int value);
    21. void onClearBntClicked();
    22. void onAgeChange(int value);
    23. private:
    24. Ui::testApp2Class ui;
    25. qPreson* boy;
    26. qPreson* girl;
    27. };

    testAPP2.cpp

    1. #include "testApp2.h"
    2. testApp2::testApp2(QWidget *parent)
    3. : QWidget(parent)
    4. {
    5. ui.setupUi(this);
    6. init();
    7. initConnect();
    8. }
    9. testApp2::~testApp2()
    10. {}
    11. void testApp2::init()
    12. {
    13. boy = new qPreson("lhm");
    14. girl = new qPreson("dq");
    15. ui.spinBoxBoyAge->setProperty("isBoy", true);
    16. ui.spinBoxgirlAge->setProperty("isBoy", false);
    17. ui.boyAgeInc->setProperty("isBoy", true);
    18. ui.girlAgeInc->setProperty("isBoy", false);
    19. boy->setProperty("age", 18);
    20. girl->setProperty("age", 17);
    21. boy->setProperty("score", 80);
    22. girl->setProperty("score", 79);
    23. }
    24. void testApp2::initConnect() {
    25. QObject::connect(ui.clear, &QPushButton::clicked, this, &testApp2::onClearBntClicked);
    26. QObject::connect(boy, &qPreson::ageChanged, this, &testApp2::onAgeChange);
    27. QObject::connect(girl, &qPreson::ageChanged, this, &testApp2::onAgeChange);
    28. QObject::connect(ui.spinBoxBoyAge,static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),this, &testApp2::onAgeSpinClicked);
    29. QObject::connect(ui.spinBoxgirlAge, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),this, &testApp2::onAgeSpinClicked);
    30. QObject::connect(ui.boyAgeInc, &QPushButton::clicked, this, &testApp2::onAgeIncClicked);
    31. QObject::connect(ui.girlAgeInc, &QPushButton::clicked, this, &testApp2::onAgeIncClicked);
    32. QObject::connect(ui.showClassInf, &QPushButton::clicked, this, &testApp2::showClassInf);
    33. }
    34. void testApp2::onClearBntClicked() {
    35. ui.textEdit->clear();
    36. }
    37. void testApp2::onAgeChange(int x) {
    38. QObject* senderPtr = qobject_cast(sender());
    39. QString information = senderPtr->property("name").toString()+" age :"+senderPtr->property("age").toString()+"\n";
    40. ui.textEdit->append(information);
    41. }
    42. void testApp2::onAgeIncClicked() {
    43. qPreson* senderPtr;
    44. if (qobject_cast(sender())->property("isBoy").toBool()) {
    45. senderPtr = boy;
    46. }
    47. else {
    48. senderPtr = girl;
    49. }
    50. senderPtr->setAge(senderPtr->getAge() + 1);
    51. }
    52. void testApp2::onAgeSpinClicked(int value) {
    53. qPreson* senderPtr;
    54. if (qobject_cast(sender())->property("isBoy").toBool()) {
    55. senderPtr = boy;
    56. }
    57. else {
    58. senderPtr = girl;
    59. }
    60. senderPtr->setAge(value);
    61. }
    62. void testApp2::showClassInf() {
    63. const QMetaObject* meta = girl->metaObject();
    64. QString inf;
    65. inf.append("----MeatObjectInformation-----\n");
    66. inf.append(QString("typename: %1\n").arg(meta->className()));
    67. for (int rcy = 1; rcy <= meta->classInfoCount()+1; rcy++) {
    68. QMetaProperty prop = meta->property(rcy);
    69. const char* propName = prop.name();
    70. QString propValue = girl->property(propName).toString();
    71. inf.append(QString("property name: %1 - property value %2\n").arg(propName, propValue));
    72. }
    73. ui.textEdit->append(inf);
    74. }

    qPreson.h:

    1. #pragma once
    2. #include
    3. #include
    4. class qPreson :public QObject{
    5. Q_OBJECT
    6. Q_CLASSINFO("author", "LHM")
    7. Q_CLASSINFO("data", "2022/11/27")
    8. Q_PROPERTY(unsigned age READ getAge WRITE setAge NOTIFY ageChanged)
    9. Q_PROPERTY(QString name MEMBER name)
    10. Q_PROPERTY(int score MEMBER score)
    11. private:
    12. QString name;
    13. int age = 0, score = 0;
    14. public:
    15. explicit qPreson(const QString& name) {
    16. this->name = name;
    17. qDebug() << this->name;
    18. }
    19. void setAge(unsigned value) {
    20. this->age = value;
    21. emit ageChanged(value);
    22. }
    23. unsigned getAge() {
    24. return age;
    25. }
    26. signals:
    27. void ageChanged(int);
    28. };

    收获总结:

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

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

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

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

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

  • 相关阅读:
    服务器(windows Server 2019为例)中的日志中文乱码的解决办法
    7分钟了解ChatGPT是如何运作的
    Spring Boot 集成freemarker模板引擎
    图像处理——数组变换
    Win:将一般用户添加到 Local Admins 组中
    Python 采集87个手绘风格PPT模板
    mysql基础(4)
    Socks5代理:数字化时代的技术支柱
    Seata详解
    linux 环境变量详解/etc/proflie
  • 原文地址:https://blog.csdn.net/weixin_62953519/article/details/128066426