Qt signal slot

Under the Qt framework, the signal slot mechanism is used to complete event processing.

The signal is the event that occurred, and the slot is the function that handles the event. The two are not together, unlike MFC's message loop mechanism. They are loosely coupled. To connect them, you must use the connect function to connect them. For example, when I want to achieve it, clicking the close button will close the current window operation. Then I need to use the connect function to connect the "click" signal function and the "close" slot function.

Below is my code:

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H

#include <QPushButton>
#include<QString>

class MyPushButton : public QPushButton
{
    Q_OBJECT
public:
    explicit MyPushButton(const int& x,const int& y,const QString& str,QWidget *parent = nullptr);
signals:

};

MyPushButton::MyPushButton(const int& x, const int& y,const QString& str,QWidget *parent) :QPushButton(parent)
{
    this->setText(str);
    this->move(x,y);
    this->resize(100,50);
}

#endif // MYPUSHBUTTON_H
#include "widget.h"
#include"mypushbutton.h"
#include <QApplication>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    
    QString str = "关闭";
    MyPushButton button(300,200,str);
    button.setParent(&w);
    
    //连接信号槽
    QObject::connect(&button,&MyPushButton::clicked,&QApplication::quit);
    
    button.show();
    w.show();
    return a.exec();
}

The core of this code lies in the use of QObject :: connect () function. Only by writing QObject in the Widget class can we use the signal slot mechanism. Therefore, connect is a function member of QObject. We can see in the help documentation that the connect function has 6 overloaded functions. Five of them are static member functions, and the remaining one is an ordinary member function. The following is the prototype of this ordinary connect function:

QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type = Qt::AutoConnection) const

This ordinary member function needs to use a QObject to call, but we really do not need to create a QObject object, so generally use the overloaded 5 static member functions.

static QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)


static QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)


static QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)


static QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)


static QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type = Qt::AutoConnection)

Looking closely at these connect functions, you can see that the sender and receiver are QObject pointer types (that is, they can be QObject objects or QObject-derived class objects). The connection type has a default value of Qt :: AutoConnection. Signals and slots can be of const char * type, const QMetaMethod & type, or member function pointer (PointerToMemberFunction), or Functor type. This type can be any function or functor that can be connected to the signal. lambda expression.

注意:Qt::UniqueConnections do not work for lambdas, non-member functions and functors; they only apply to connecting to member functions.

Obviously, the fourth function in the static function is used in my code.

 QObject::connect(&button,&MyPushButton::clicked,&QApplication::quit);

button is the signal sender, clicked is the signal, and quit is the slot function.

When we click the button and send a signal, the slot function quit will be called and the program exits.

For lambda expressions, please see here: https://blog.csdn.net/zy010101/article/details/73613664

The signal slot requires that the parameters of the signal and the slot are the same. If they are inconsistent, the parameters of the slot function can be less than those of the signal. Even so, the order of the parameters of the slot function must be consistent with the previous parameters of the signal. Because, you can choose to ignore the data from the signal in the slot function, but you can't say that the signal does not have this parameter at all, you will use it in the slot function.

The form of the connect function we generally use is as follows:

connect(sender, signal, receiver, slot);

sender is the signal sender, signal is the signal, receiver is the signal receiver, and slot is the slot function.

When the signal is sent, the slot function will be automatically called.

 

 

 

 

 

 

Published 242 original articles · Like 180 · Visits 160,000+

Guess you like

Origin blog.csdn.net/zy010101/article/details/105334793