• QT/自定义槽和信号


    创建两个类student和teacher,父类都为QObject

    在teacher中自定义Signal,hungry

    1. //自定义信号,写道signals下
    2. signals:
    3. //返回值void
    4. //只需要声明
    5. //可以有参数,可以重载
    6. void hungry();
    7. void hungry(QString FoodName);

    在student中自定义Slot,treat

    1. //自定义槽函数,写到public slots Qt5.0版本以上可以写成全局函数或者public作用域下或者lambda表达式
    2. public slots:
    3. //返回值void
    4. //需要声明和实现
    5. //可以有参数,可以重载
    6. void treat();
    7. void treat(QString FoodName);

    当teacher对象发出hungry信号,调用treat槽函数

    connect(tc, &teacher::hungry, st, &student::treat);

    当遇到重载的槽函数时,需用函数指针指向对应的函数

    1. //连接信号槽
    2. //连接有参数信号和槽
    3. //当信号和槽发生重载的时候,函数指针可以指向函数地址,用函数指针明确对应的函数
    4. void(teacher::*teacherSignal)(QString) = &teacher::hungry;
    5. void(student::*studentSlot)(QString) = &student::treat;
    6. void(student::*studentSlotEmpty)() = &student::treat;
    7. connect(tc, teacherSignal, st, studentSlot);
    8. connect(btn1, &QPushButton::clicked, st, studentSlotEmpty);

    Qstring转换为char类型的方法

    1. //QString转char* 通过.toUtf8转为QByteArrary类型通过.data()转为char*
    2. qDebug() << "请老师吃" << FoodName.toUtf8().data();

    student.h

    1. #ifndef STUDENT_H
    2. #define STUDENT_H
    3. #include
    4. #include
    5. class student : public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit student(QObject *parent = nullptr);
    10. signals:
    11. //自定义槽函数,写到public slots Qt5.0版本以上可以写成全局函数或者public作用域下或者lambda表达式
    12. public slots:
    13. //返回值void
    14. //需要声明和实现
    15. //可以有参数,可以重载
    16. void treat();
    17. void treat(QString FoodName);
    18. void treat(QString FoodName, QString SecondFoodName);
    19. };
    20. #endif // STUDENT_H

    teacher.h

    1. #ifndef TEACHER_H
    2. #define TEACHER_H
    3. #include
    4. #include
    5. class teacher : public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit teacher(QObject *parent = nullptr);
    10. //自定义信号,写道signals下
    11. signals:
    12. //返回值void
    13. //只需要声明
    14. //可以有参数,可以重载
    15. void hungry();
    16. void hungry(QString FoodName);
    17. };
    18. #endif // TEACHER_H

    widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include "mypushbutton.h"
    7. #include "teacher.h"
    8. #include "student.h"
    9. QT_BEGIN_NAMESPACE
    10. namespace Ui { class Widget; }
    11. QT_END_NAMESPACE
    12. class Widget : public QWidget
    13. {
    14. Q_OBJECT
    15. public:
    16. Widget(QWidget *parent = nullptr);
    17. ~Widget();
    18. teacher* tc;
    19. student* st;
    20. QPushButton* btn1;
    21. QPushButton* btn2;
    22. QPushButton* btn3;
    23. QPushButton* btn4;
    24. void display(QTextEdit *text1);
    25. void classIsOver();
    26. private:
    27. Ui::Widget *ui;
    28. };
    29. #endif // WIDGET_H

    widget.cpp

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. //new时括号内加入this,会将对象放入对象树,会自动调用析构函数
    9. this->tc = new teacher(this);
    10. this->st = new student(this);
    11. this->btn1 = new QPushButton;
    12. this->btn2 = new QPushButton;
    13. this->btn3 = new QPushButton("close", this);
    14. this->btn4 = new QPushButton("吃两个菜", this);
    15. btn1->setParent(this);
    16. btn1->setText("下课");
    17. btn2->setParent(this);
    18. btn2->setText("display");
    19. btn2->move(0,200);
    20. btn3->move(0,300);
    21. btn4->move(0,250);
    22. //连接信号槽
    23. //连接有参数信号和槽
    24. //当信号和槽发生重载的时候,函数指针可以指向函数地址,用函数指针明确对应的函数
    25. void(teacher::*teacherSignal)(QString) = &teacher::hungry;
    26. void(student::*studentSlot)(QString) = &student::treat;
    27. connect(tc, teacherSignal, st, studentSlot);
    28. //信号可以连接信号
    29. void(teacher::*teacherSignalEmpty)() = &teacher::hungry;
    30. void(student::*studentSlotEmpty)() = &student::treat;
    31. connect(tc, teacherSignalEmpty, st, studentSlotEmpty);
    32. connect(btn1, &QPushButton::clicked, tc, teacherSignalEmpty);
    33. //信号可以断开连接
    34. //disconnect(btn1, &QPushButton::clicked, tc, teacherSignalEmpty);
    35. //一个信号可以绑定多个槽函数
    36. //多个信号可以绑定同一个槽函数
    37. connect(btn2, &QPushButton::clicked, tc, teacherSignalEmpty);
    38. //槽函数的参数和信号的参数类型必须一一对应
    39. //信号的参数个数可以多余槽函数的参数个数,反之不可以
    40. //链接槽函数 注:信号的参数要大于等于槽函数的参数,
    41. //点击事件无参,但是槽函数需要传递ui参数
    42. //故槽函数采用Qt5之后支持的lambda表达式进行传参,常用=传递值,而不是&,因为当槽和信号连接时,当前控件会进入锁的状态,而=传递是创造新的值覆盖,不会过多影响效率,加上mutable关键字,可以修改按值传递的拷贝
    43. //新建一个textedit
    44. QTextEdit *text1 = new QTextEdit(this);
    45. //设置大小为200*100并放置在(300,100)处
    46. text1->resize(200, 100);
    47. text1->move(300, 100);
    48. //重置窗体大小为600*400
    49. this->resize(600, 400);
    50. connect(btn2, &QPushButton::clicked, text1, [=](){display(text1);});
    51. //Qt4版本信号和槽信号写法(不推荐)
    52. //利用Qt4版本连接有参信号和槽
    53. //优势参数直观
    54. //劣势参数类型不做匹配检测
    55. //connect(tc, SIGNAL(hungry(QString), st, SLOT(treat(QString)));
    56. //点击按钮,关闭窗口
    57. connect(btn3, &QPushButton::clicked, this, [=](){this->close();});
    58. connect(btn4, &QPushButton::clicked, this, [=](){this->st->treat("宫保鸡丁", "鱼香肉丝");});
    59. classIsOver();
    60. }
    61. void Widget::display(QTextEdit *text1)
    62. {
    63. text1->setText("I Love you more than I can say!");
    64. text1->append("我爱你在心口难开");
    65. }
    66. void Widget::classIsOver()
    67. {
    68. //触发自定义信号,emit是Qt关键字,使用:在A中对B发出信号
    69. emit this->tc->hungry("宫保鸡丁");
    70. }
    71. Widget::~Widget()
    72. {
    73. delete ui;
    74. }

  • 相关阅读:
    【附源码】Python计算机毕业设计网上购物系统
    Nginx惊群问题分析及解决
    懒人系列--文件上传之OSS使用案例
    PIL(Python Imaging Library)图像处理库教程
    【数学建模】混合整数规划MIP(Python+Gurobi代码实现)
    什么是商业?什么是企业?什么又是竞争呢?
    MindSpore:强化学习基础-蒙特卡洛(Monte Carlo)
    【小程序】微信小程序常用api的使用,附案例(建议收藏)
    服务器和电脑的区别
    com.squareup.okhttp3:okhttp 组件安全漏洞及健康度分析
  • 原文地址:https://blog.csdn.net/m0_56654156/article/details/136264294