zoukankan      html  css  js  c++  java
  • 【linux】【qt5】【信号槽示例】

    什么叫信号槽:

      信号槽是 Qt 框架引以为豪的机制之一。所谓信号槽,实际就是观察者模式。当某个事件发生之后,比如,按钮检测到自己被点击了一下,它就会发出一个信号(signal)。这种发出是没有目的的,类似广播。如果有对象对这个信号感兴趣,它就会使用连接(connect)函数,意思是,将想要处理的信号和自己的一个函数(称为槽(slot))绑定来处理这个信号。也就是说,当信号发出时,被连接的槽函数会自动被回调。这就类似观察者模式:当发生了感兴趣的事件,某一个操作就会被自动触发。

    信号槽的作用:

      一般情况下信号槽做监视作用,对于新手则更多是窗体间的信息传递。父窗体点击事件在子窗体显示文本,子窗体点击事件关闭父窗体之类的。

    信号槽格式:

      connect(sender, signal, receiver, slot);

      connect(Sender,SIGNAL(signal),Receiver,SLOT(slot));

      connect(sender,SIGNAL(signal),receiver,SLOT(slot),Qt::DirectConnection);

      参数:

    1.    sender:发出信号的对象(一般为ui控件或者窗体对象)。
    2.    signal:发送对象发出的信号(程序运行到这个地方,就会执行槽函数)。
    3.    receiver:接收信号的对象(那个对象接受信号,本窗体的话就是this)。
    4.    slot:接收对象在接收到信号之后所需要调用的函数(槽函数)。

    声明:

      signals:

        void sendData(QString);//声明信号。

      private slots:
        void receiveData(QString data);//声明槽函数。

     

    信号槽的格局:

      一个信号可以对应多个槽函数,多个信号也可以对应一个槽函数。

     

    示例:

      点击父窗体的按钮,将获取到的文本框的值传给子窗体。

     

     1 #ifndef DIALOG_H
     2 #define DIALOG_H
     3 
     4 #include <QDialog>
     5 #include "dialog2.h"
     6 
     7 namespace Ui {
     8 class Dialog;
     9 }
    10 
    11 class Dialog : public QDialog
    12 {
    13     Q_OBJECT
    14 
    15 public:
    16     explicit Dialog(QWidget *parent = 0);
    17     ~Dialog();
    18 
    19 private slots:
    20     void on_pushButton_clicked();
    21 
    22 signals:
    23     void sendData(QString);//定义信号
    24 
    25 private:
    26     Ui::Dialog *ui;
    27     Dialog2 *dlg2;//定义子窗体对象
    28 };
    29 
    30 #endif // DIALOG_H
    View Code

     

     1 #include "dialog.h"
     2 #include "ui_dialog.h"
     3 
     4 
     5 
     6 Dialog::Dialog(QWidget *parent) :
     7     QDialog(parent),
     8     ui(new Ui::Dialog)
     9 {
    10     ui->setupUi(this);
    11 
    12     dlg2 = new Dialog2(this);//对象用new是为了实现非模态对话框,this用了对象树(即非模态对话框父窗体关闭,子窗体也关闭)
    13     connect(this, SIGNAL(sendData(QString)), dlg2, SLOT(receiveData(QString)));//构造一个信号槽,这个多方构造函数里面。
    14 }
    15 
    16 Dialog::~Dialog()
    17 {
    18     delete ui;
    19 }
    20 
    21 void Dialog::on_pushButton_clicked()
    22 {
    23     emit sendData(ui->lineEdit->text());  //获取lineEdit的输入并且发射信号
    24     dlg2->show();//show出子窗体
    25 }
    View Code
     1 #ifndef DIALOG2_H
     2 #define DIALOG2_H
     3 
     4 #include <QDialog>
     5 
     6 namespace Ui {
     7 class Dialog2;
     8 }
     9 
    10 class Dialog2 : public QDialog
    11 {
    12     Q_OBJECT
    13 
    14 public:
    15     explicit Dialog2(QWidget *parent = 0);
    16     ~Dialog2();
    17 
    18 private slots:
    19     void receiveData(QString data);//槽函数,接受到信号后做处理
    20 
    21 private:
    22     Ui::Dialog2 *ui;
    23 };
    24 
    25 #endif // DIALOG2_H
    View Code
     1 #include "dialog2.h"
     2 #include "ui_dialog2.h"
     3 
     4 Dialog2::Dialog2(QWidget *parent) :
     5     QDialog(parent),
     6     ui(new Ui::Dialog2)
     7 {
     8     ui->setupUi(this);
     9 }
    10 
    11 Dialog2::~Dialog2()
    12 {
    13     delete ui;
    14 }
    15 
    16 void Dialog2::receiveData(QString data)
    17 {
    18     ui->textEdit->setText(data);     //获取传递过来的数据
    19 }
    View Code

      父窗体里面2个控件:按钮和输入框。

      子窗体一个空间:文本框。

    信号槽的断开:

      信号槽在出生就一直处于监视状态,一旦有信号就执行槽函数,有时间我们不想让他执行怎么办,那就断开这个信号槽吧。

      [static] bool QObject::disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)

      disconnect(myObject, 0, 0, 0);
       myObject->disconnect();
      disconnect(myObject, SIGNAL(mySignal()), 0, 0);
      myObject->disconnect(SIGNAL(mySignal()));
      disconnect(myObject, 0, myReceiver, 0);

      想让信号槽只执行一次,那就把断开放到槽函数里面吧。执行到槽函数就断开,哈哈。

    后记:

      信号槽对于喜欢qt的是在是太重要了。对于qt也是太重要了,但这个核心也不是一天两天就能学会的,上面示例也就是起到抛砖引玉的效果。先挺过过去吧。

      读者加油。

      

     

     

     

     

     

  • 相关阅读:
    内存泄漏检测工具VLD在VS2010中的使用举例
    boost::threadpool 调用类成员变量并传入参数 的方法
    boost之ThreadPool
    DllMain 用法
    分布式锁的几种实现方式
    利用cbmakegen导出Code::blocks的Makefile
    搜集C++实现的线程池
    微软开源rDSN分布式系统开发框架
    腾讯互娱开源分布式开发框架Pebble
    SpringBoot指定额外需要扫描的包
  • 原文地址:https://www.cnblogs.com/13373-/p/11466610.html
Copyright © 2011-2022 走看看