Custom Signal and Slot Function for QT

Keywords: Qt Qt5

Signals and slots are a major innovation of QT. Custom signals and slots can achieve their desired functions.
The standard signal and slot writing is as follows:
*Description of the connect ion signal and slot above
* &button: Signal sender, pointer type
* &QPushButton::clicked: Processing signal, &Sender's class name:: Signal name (without parentheses, just name)
* this: Receiver, pointer type
* &QWidget::close:&Receiver's Class Name::Slot Function Name

Custom slots, usage of common functions
Qt5: Any member function, general global function, static function
Slot function needs to be consistent with signal (parameter, return value)
Since the signals have no return value, the slot function generally has no return value

Custom signal:
Signal must have signals keyword to declare
Signal has no return value but can have parameters
A signal is a declaration of a function, it only needs to be declared, it does not need to be defined
Use: emit (usually to send a signal through a custom slot function)

Introduction: The outline of the code below is to define a B2 button in the main window, define a signal and slot to open the child window s1, then define two buttons sb1 and s B2 in the child window s1, which triggers a custom sb1sendSlot slot function (this slot function is used to send sb1Signal() signals), similar to s b2, to enable signal broadcasting in the child window, and the parent window to thisThe slot functions of the parent window are triggered by signals of interest.(Because the parent window can control the word window, the child window cannot control the parent window in turn (there is no pointer to the parent in the child window), it can only send a signal broadcast to interest the parent)

Paste code:
Headers file:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "subwidget.h"
namespace Ui {
class Widget;

class Widget : public QWidget

    explicit Widget(QWidget *parent = 0);
    subWidget s1;   //Header file introduces subwidget.h, a QWidget library, and instantiates object s1


    Ui::Widget *ui;
    QPushButton *btn=new QPushButton;
    QPushButton b2;


#endif // WIDGET_H



#include <QWidget>
class subWidget : public QWidget
    explicit subWidget(QWidget *parent = nullptr);
    QPushButton sb1;
    QPushButton sb2;
    void sb1sendSlot();
    void sb2sendSlot();
    void sb1Signal();
    void sb2Signal();
public slots:


#endif // SUBWIDGET_H

Sources file:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
    QApplication a(argc, argv);
    Widget w;;

    return a.exec();


#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    ui(new Ui::Widget)

    btn->setParent(this);  //btn is new in widget.h, it's a pointer, so use function - >
    btn->setText("Close main window");
    this->resize(400,500);  //This represents the object that calls this function, the widget, the main window
     * Description of connect ion signal and slot above
     * btn : Signal sender, pointer type
     * &QPushButton::clicked : Signal processed, &Sender's class name:: Signal name
     * this : Receiver, pointer type
     * &QWidget::close : &Receiver's Class Name: Slot Function Name

    b2.setParent(this);  //The b2 button is the button declared by widget.h, where the parent object is specified as this main window
    b2.setText("Open a new window");
    connect(&b2,&QPushButton::released,&s1,&subWidget::show); //Press the b2 button in the main window and s1 in the secondary window will show

    //***The following two Connes are receiving custom signals
     * sb1Signal Is through a custom slot function sb1sendSlot Signal,
     * The child window broadcasts when it comes out. This one belowthisThe slot function is called when the main window is interested

    delete ui;


#include "subwidget.h"

subWidget::subWidget(QWidget *parent) : QWidget(parent)
    //***sb1 and sb2 are the two buttons declared in subWidget.h
   sb1.setText("Close Main Window 2");
    * To the above connect Function declaration
    * &sb1 : Signal sender, pointer type
    * &QPushButton::clicked : Processed signal, &Signal sender type:Signal name
    * this : Receiver, pointer type
    * &subWidget::sb1sendSlot : Slot function, signal processing function &Receiver Class Name: Slot Function Name
    * Note: This slot function name is custom, and is.h InpublicStatement belowvoidFunction, and then.cpp Define below.
   sb2.setText("Show Main Window");

void subWidget::sb1sendSlot()  //This function is a custom slot function and needs to be scoped with a class name. Define it here
    emit sb1Signal();   //This signal is sent in this slot function, which is declared in the.h signal and sent with emit before it

void subWidget::sb2sendSlot()
    emit sb2Signal();

Summary: It feels like writing code can debug, write clearly, and drag control does not know how to implement, and object can not be found for later modification. So it is recommended to write code to interface for later maintenance. Of course, you can also design drag control to achieve the same function first, then write code to achieve the same function.

Thirteen original articles were published. Zambia 1. Visits 3959
Private letter follow

Posted by niki on Sun, 01 Mar 2020 17:58:22 -0800