信号槽(重点)

信号函数与槽函数是Qt在C++的基础上新增的特性,类似于其它面向对象编程中的回调函数,这种机制可以先预设好要执行的代码,在代码运行的过程中,一旦达成某个条件,系统自动执行之前预设的代码,是一种组件之间通信的方式。
在上面的例子中,信号函数充当条件,槽函数充当要执行的代码。
信号函数→槽函数
也就是说,程序中信号函数“发射”后,槽函数按照之前的约定自动触发,无需程序员手动编码调用。

1. 使用方式

1.1 使用条件

  • 通信的对象必须继承自QObject类,QObject是所有Qt类的基类。
  • 类的内部必须加入Q_OBJECT宏

    1.2 函数原型

    信号槽的使用最重要的函数是QObject类中的静态成员函数connect:
    三、信号槽(重点) - 图1

  • 参数1:发射者,触发条件的对象,是发射信号函数的对象

  • 参数2:信号函数,触发的条件,可以在文档中查找;需要在函数名外面包裹SIGNAL()
  • 参数3:接收者,触发条件后执行槽函数代码的对象
  • 参数4:槽函数,触发条件后执行的代码,可以在文档中查找;需要在函数名外面包裹SLOT()

    2. 三种实现方式

    信号槽的触发方式有多种,为了讲课方便,复杂程序循序渐进依次为:

  • 自带信号→自带槽

  • 自带信号→自定义槽
  • 自定义信号→自定义槽

    2.1 自带信号→自带槽

    这种方式下,信号函数使用Qt官方自带的函数,槽函数也使用Qt官方自带的函数,我们需要做的只是查阅文档找到对应函数后,调用connect函数连接即可。
    【例子】点击按钮,关闭窗口。
    分析:

  • 参数1:发射者,按钮对象

  • 参数2:信号函数,点击函数 void clicked(bool checked = false)
  • 参数3:接收者,窗口对象
  • 参数4:槽函数,关闭函数 bool close()

代码如下:
dialog.h

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include <QPushButton>
  5. class Dialog : public QDialog
  6. {
  7. Q_OBJECT
  8. public:
  9. Dialog(QWidget *parent = 0);
  10. ~Dialog();
  11. private:
  12. QPushButton *btn;
  13. };
  14. #endif // DIALOG_H

dialog.cpp

  1. #include "dialog.h"
  2. Dialog::Dialog(QWidget *parent)
  3. : QDialog(parent)
  4. {
  5. resize(400,400);
  6. btn = new QPushButton("关闭",this);
  7. btn->move(100,150);
  8. // 信号槽连接,连接是一种约定,不是触发
  9. connect(btn,SIGNAL(clicked()),this,SLOT(close()));
  10. }
  11. Dialog::~Dialog()
  12. {
  13. delete btn;
  14. }

以上代码本身的编写相对不那么重要,重要的是整个分析和查找的流程!

2.2 自带信号→自定义槽

触发的槽函数代码Qt官方不一定能准备好,因此对于一些非常规的、复杂的槽函数需要开发者手动编写。这种信号槽的连接方式是使用频率最多的一种。
【例子】点击按钮,移动窗口并输出当前的窗口坐标。
分析:
这种情况的槽函数需要手动编写,槽函数也是成员函数,但是是一种特殊格式的成员函数。
dialog.h

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include <QPushButton>
  5. #include <QDebug>
  6. class Dialog : public QDialog
  7. {
  8. Q_OBJECT
  9. public:
  10. Dialog(QWidget *parent = 0);
  11. ~Dialog();
  12. private:
  13. QPushButton *btn;
  14. private slots:
  15. // 槽函数声明
  16. void mySlot();
  17. };
  18. #endif // DIALOG_H

dialog.cpp

  1. #include "dialog.h"
  2. Dialog::Dialog(QWidget *parent)
  3. : QDialog(parent)
  4. {
  5. resize(300,300);
  6. btn = new QPushButton("移动窗口",this);
  7. // 连接信号槽
  8. // 发射者:按钮对象btn
  9. // 信号:点击clicked
  10. // 接收者:this
  11. // 槽:mySlot()
  12. connect(btn,SIGNAL(clicked()),this,SLOT(mySlot()));
  13. }
  14. // 定义槽函数
  15. void Dialog::mySlot()
  16. {
  17. // 获得当前窗口的位置
  18. int x = this->x();
  19. int y = this->y();
  20. // 移动
  21. move(x+10,y+10);
  22. // 输出当前的窗口坐标
  23. qDebug() << x+10 << "*" << y+10;
  24. }
  25. Dialog::~Dialog()
  26. {
  27. delete btn;
  28. }

2.3 自定义信号→自定义槽

注意,因为现在是初学阶段,这种方式无法举一个完全合适的例子,因此下面演示案例不是最优解。我们只需要站在语法的角度看待即可。
自定义信号比较特殊:

  • 没有权限
  • 只有声明,没有定义

【例子】点击按钮发射自定义信号,另一个槽函数接收此信号后输出一句话。
分析:
点击按钮 → 自定义槽函数1(发射自定义信号)
自定义信号→自定义槽函数2(输出)
代码如下:
dialog.h

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include <QPushButton>
  5. #include <QDebug>
  6. class Dialog : public QDialog
  7. {
  8. Q_OBJECT
  9. public:
  10. Dialog(QWidget *parent = 0);
  11. ~Dialog();
  12. private:
  13. QPushButton *btn;
  14. private slots:
  15. void mySlot1(); // 槽函数1
  16. void mySlot2(); // 槽函数2
  17. // 自定义信号:只声明
  18. signals:
  19. void mySignal();
  20. };
  21. #endif // DIALOG_H

dialog.cpp

  1. #include "dialog.h"
  2. Dialog::Dialog(QWidget *parent)
  3. : QDialog(parent)
  4. {
  5. resize(200,200);
  6. btn = new QPushButton("方式3",this);
  7. // 连接第一个信号槽
  8. connect(btn,SIGNAL(clicked()),this,SLOT(mySlot1()));
  9. // 连接第二个槽函数
  10. connect(this,SIGNAL(mySignal()),this,SLOT(mySlot2()));
  11. }
  12. void Dialog::mySlot1()
  13. {
  14. //发射自定义信号
  15. emit mySignal();
  16. }
  17. void Dialog::mySlot2()
  18. {
  19. qDebug() << "兜兜转转,还是触发了槽函数2";
  20. }
  21. Dialog::~Dialog()
  22. {
  23. delete btn;
  24. }

3. 参数传递

后续还会有其它的传递参数的方式,编程需要灵活运用,此处先介绍两种方式。

3.1 全局参数

【例子】点击按钮,按钮上显示点击的次数。
dialog.h

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include <QPushButton>
  5. class Dialog : public QDialog
  6. {
  7. Q_OBJECT
  8. public:
  9. Dialog(QWidget *parent = 0);
  10. ~Dialog();
  11. private:
  12. QPushButton *btn;
  13. // 全局参数:点击的次数
  14. int count = 0;
  15. private slots:
  16. void countSlot();
  17. };
  18. #endif // DIALOG_H

dialog.cpp

  1. #include "dialog.h"
  2. Dialog::Dialog(QWidget *parent)
  3. : QDialog(parent)
  4. {
  5. resize(150,100);
  6. btn = new QPushButton("0",this);
  7. // 点击按钮 → 自定义槽函数:计数并显示
  8. connect(btn,SIGNAL(clicked()),this,SLOT(countSlot()));
  9. }
  10. void Dialog::countSlot()
  11. {
  12. count++;
  13. // QString类型是Qt的字符串类型
  14. // int → QString 的函数:QString number(int n, int base = 10)
  15. QString text = QString::number(count);
  16. btn->setText(text);
  17. }
  18. Dialog::~Dialog()
  19. {
  20. // Qbject中的一个槽函数,主要与直接delete的区别在于销毁对象的时候比较温和
  21. btn->deleteLater();
  22. }

3.2 信号槽传参

信号发射时可以携带参数,槽函数可以接收参数。注意,因为现在是初学阶段,这种方式无法举一个完全合适的例子,因此下面演示案例不是最优解。我们只需要站在语法的角度看待即可。
【例子】点击按钮,触发自定义槽函数1,在自定义槽函数1中发射自定义信号,自定义信号携带点击的次数作为参数,发射到自定义槽函数2,在自定义槽函数2中接收参数并显示在按钮上。
代码如下:
dialog.h

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include <QPushButton>
  5. class Dialog : public QDialog
  6. {
  7. Q_OBJECT
  8. public:
  9. Dialog(QWidget *parent = 0);
  10. ~Dialog();
  11. private:
  12. QPushButton *btn;
  13. private slots:
  14. void mySlot1(); // 槽函数1
  15. void mySlot2(int); // 槽函数2,注意哑元参数位
  16. signals: // 信号函数,注意哑元参数位
  17. void mySignal(int);
  18. };
  19. #endif // DIALOG_H

dialog.cpp

  1. #include "dialog.h"
  2. Dialog::Dialog(QWidget *parent)
  3. : QDialog(parent)
  4. {
  5. resize(150,100);
  6. btn = new QPushButton("0",this);
  7. connect(btn,SIGNAL(clicked()),this,SLOT(mySlot1()));
  8. connect(this,SIGNAL(mySignal(int)),this,SLOT(mySlot2(int)));
  9. }
  10. void Dialog::mySlot1()
  11. {
  12. static int count = 0; // 次数
  13. //携带参数发射信号
  14. emit mySignal(++count);
  15. }
  16. /**
  17. * @brief Dialog::mySlot2
  18. * @param count 信号发来的参数
  19. */
  20. void Dialog::mySlot2(int count)
  21. {
  22. // 转换类型
  23. QString text = QString::number(count);
  24. btn->setText(text);
  25. }
  26. Dialog::~Dialog()
  27. {
  28. delete btn;
  29. }

信号槽传参需要注意以下几点:
1. 可以传递任意多个参数
2. 信号的参数个数必须大于等于槽的参数个数
3. 参数类型必须一一匹配

4. 对应关系

一对多:同一个信号可以连接多个不同的槽。
多对一:多个不同的信号可以连接同一个槽。
dialog.h

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include <QPushButton>
  5. #include <QDebug>
  6. class Dialog : public QDialog
  7. {
  8. Q_OBJECT
  9. public:
  10. Dialog(QWidget *parent = 0);
  11. ~Dialog();
  12. private:
  13. QPushButton *btn;
  14. QPushButton *btn2;
  15. QPushButton *btn3;
  16. QPushButton *btn4;
  17. private slots:
  18. void mySlot1();
  19. void mySlot2();
  20. void mySlot3();
  21. };
  22. #endif // DIALOG_H

dialog.cpp

  1. #include "dialog.h"
  2. Dialog::Dialog(QWidget *parent)
  3. : QDialog(parent)
  4. {
  5. resize(200,400);
  6. btn = new QPushButton("一对多",this);
  7. btn2 = new QPushButton("多对一",this);
  8. btn2->move(0,100);
  9. btn3 = new QPushButton("多对一",this);
  10. btn3->move(0,200);
  11. btn4 = new QPushButton("多对一",this);
  12. btn4->move(0,300);
  13. //一对多
  14. connect(btn,SIGNAL(clicked()),this,SLOT(mySlot1()));
  15. connect(btn,SIGNAL(clicked()),this,SLOT(mySlot2()));
  16. connect(btn,SIGNAL(clicked()),this,SLOT(mySlot3()));
  17. // 多对一
  18. connect(btn2,SIGNAL(clicked()),this,SLOT(mySlot1()));
  19. connect(btn3,SIGNAL(clicked()),this,SLOT(mySlot1()));
  20. connect(btn4,SIGNAL(clicked()),this,SLOT(mySlot1()));
  21. }
  22. void Dialog::mySlot1()
  23. {
  24. qDebug() << "1";
  25. }
  26. void Dialog::mySlot2()
  27. {
  28. qDebug() << "2";
  29. }
  30. void Dialog::mySlot3()
  31. {
  32. qDebug() << "3";
  33. }
  34. Dialog::~Dialog()
  35. {
  36. delete btn;
  37. delete btn2,btn3,btn4;
  38. }

5. Qt与C++课程中命名规范的差别

  • 变量、函数名改为驼峰命名法(小驼峰命名法)

第一个单词全小写,后面每个单词的首字母大写。

  • get函数可以只写属性名称
  • 文件名使用类名都小写,无连接符