QT study of signals and slots

Abstract: QT, we can use the functions provided by the system to achieve link signals and slots, but in the QT Not only that, but also allows users to define their own signals and slots function. In this article, I will define their own class under teacher definition signal function hungry, then student in the class definition define their own slot function treat, and finally in wedget class will link the two functions, triggers, display.

1, teacher class

Function of the signal, the present embodiment is Hungry, we have no parameters and parameters, wherein the parameters representative of the content of the function parameters to be transmitted

head File:

. 1  #ifndef TEACHER_H
 2  #define TEACHER_H
 . 3  
. 4 #include <QObject>
 . 5  
. 6  class Teacher: public QObject
 . 7  {
 . 8      the Q_OBJECT
 . 9  public :
 10      Explicit Teacher (QObject * parent = 0 );
 . 11  
12 is  // signal signal writing custom to the lower signals 
13 is  signals:
 14      // signal return value is void
 15      // signals need not be implemented only declare 
16      void Hungry ();
 . 17  
18 is      voidHungry (QString foodName);
 . 19  
20 is  // groove slots function may be written public 
21 is  public slots:
 22 is  };
 23 is  
24  #endif  // TEACHER_H

Source File:

1 #include "teacher.h"
2 
3 Teacher::Teacher(QObject *parent) : QObject(parent)
4 {
5 }

2, student class

Slot design and implement a function for receiving the signal

head File:

. 1  #ifndef STUDENT_H
 2  #define STUDENT_H
 . 3  
. 4 #include <QObject>
 . 5  
. 6  class Student: public QObject
 . 7  {
 . 8      the Q_OBJECT
 . 9  public :
 10      Explicit Student (QObject * parent = 0 );
 . 11  Signals:
 12 is  public slots:
 13 is      // treat the slot function must implement and the function groove is hungry and the corresponding 
14      void treat ();
 15      void treat (QString foodName);
 16  };
 . 17  
18 is #endif // STUDENT_H

Source File:

. 1 #include " student.h " 
2 #include <QDebug>
 . 3 Student Student :: (QObject * parent): QObject (parent)
 . 4  {
 . 5  
. 6  }
 . 7  
. 8  void Student :: Treat ()
 . 9  {
 10      qDebug () < < " ask the teacher eat! " ;
 . 11  
12 is  }
 13 is  
14  void Student :: Treat (QString foodName)
 15  {
 16      // QString -> char *
 . 17      // QString str.toUtf8 STR () Data ();. became string and without quotation marks 
18     qDebug () << " asked the teacher to eat, eat teacher:! " << . foodName.toUtf8 () the Data (); 
 19 }

3, the link signals and grooves wedget

Header: Defines the trigger function

. 1  #ifndef WIDGET_H
 2  #define WIDGET_H
 . 3  
. 4 #include <the QWidget>
 . 5 #include " student.h " 
. 6 #include " teacher.h " 
. 7  
. 8  class the Widget: public the QWidget
 . 9  {
 10      the Q_OBJECT
 . 11  
12 is  public :
 13 is      the Widget (the QWidget parent = * 0 );
 14      Student * ST; // designed to be convenient to use a member in the Connect 
15      Teacher * ZT;
 16     void classover (); // trigger function 
. 17      ~ the Widget ();
 18 is  };
 . 19  
20 is  #endif  // WIDGET_H

Source File:

And implementing a signal using the connect link slot; also achieved triggering button links, ultimately triggering slot function

Note that the distinction between different kinds of signals with the function pointer to a function, two types of functions to connect the corresponding link (corresponding to the principle parameters), the system can only correspond QPushButton :: clicked without parameters.

Located behind the trigger function to connect, and when the function achieved by means emit, the object is triggered to clear.

. 1 #include " widget.h " 
2 #include <the QPushButton>
 . 3  
. 4 the Widget :: the Widget (the QWidget * parent)
 . 5      : the QWidget (parent)
 . 6  {
 . 7      ST = new new Student ( the this );
 . 8      ZT = new new Teacher ( the this ) ;
 . 9  
10      Connect (ZT, & Teacher :: hungery, ST, & Student :: Treat);
 . 11      // use of function pointers to distinguish the different signal as a function 
12 is      void (* teacherSingal1 :: Teacher) (QString) = & Teacher: : hungery;
 13 is      void(Student::*studentSlot1)(QString)=&Student::treat;
14     void(Teacher::*teacherSingal2)(void)=&Teacher::hungery;
15     void(Student::*studentSlot2)(void)=&Student::treat;
16 //    QPushButton* btn=new QPushButton;
17 //    btn->setParent(this);
18 //    btn->setText("下课");
19 
20 //    connect(btn,&QPushButton::clicked,zt,teacherSingal2);
21     connect(zt,teacherSingal1,st,studentSlot1);
22     connect(zt,teacherSingal2,st,studentSlot2);
23     classover ();
 24  }
 25  void the Widget :: classover () {
 26 is      EMIT ZT-> hungery ();   // trigger function to achieve 
27      EMIT ZT-> hungery (QString ( " Gongbaojiding " ));
 28  }
 29  
30 the Widget :: ~ the Widget ()
 31 is  {
 32  
33 is }

4, some other little knowledge about signals and slots

1) can only link signals and slots can also be disconnected by disconnecting disconnect function

1  disconnect(zt,teacherSingal1,st,studentSlot1);

2) a plurality of grooves may trigger signal functions:

1 connect(btn,&QPushButton::clicked,zt,teacherSingal2);
2 connect(btn,&QPushButton::clicked,this,&Widget::close);

The above procedure may be implemented to display information, while closing the window

3) a plurality of signals may also be linked with a slot function

4) the parameter signals and the slot function must correspond, parameters of the signal may be more than the groove functions, not vice versa, type and position correspond to the parameters.

Guess you like

Origin www.cnblogs.com/lzy820260594/p/11352346.html