3. Signals and slots in QT --- signal connection mode, custom signals and slots, signal slot overloading, disconnection

1. Description

In fact, it is a simple way of the observer mode . The connect function can be used to establish a connection for the signal and the slot , and the slot will make corresponding processing when the signal is broadcast (triggered) . Among them, the signal comes from the observed object, and the slot comes from the observer . **Signal: **Function declaration, no need to implement **Slot: **The essence is a function, generally a member function of a class, with declaration and implementation

2. Signal Connection

The first:
use the SIGNAL and SLOT keywords, so that parameters can be passed in signals and slots

//创建按钮
QPushButton* btn = new QPushButton();
//设置位置
btn->move(150,200);
//设置大小
btn->resize(100,30);
btn->setText("窗口最大");
//设置字体
QFont font("楷体",15,1);
btn->setFont(font);
btn->setParent(this);
//设置样式
btn->setStyleSheet("QPushButton{background-color:lightblue;}\
                    QPushButton:hover{background-color:lightgreen;}\
                    QPushButton:pressed{background-color:rgba(170,155,221,1);}");
btn->show();

//创建信号连接(第一中方式:使用SIGNAL和SLOT关键字,可传递参数)
connect(btn,SIGNAL(clicked(bool)),this,SLOT(showMaximized()));

The second method:
using address scope, this method is not very friendly when there are multiple signals and slot functions

auto btnMin = new QPushButton("窗口正常",this);
btnMin->move(100,100);
//设置大小
btnMin->resize(100,30);
//设置字体
QFont font2("楷体",15,1);
btnMin->setFont(font2);
btnMin->setParent(this);
//设置样式
btnMin->setStyleSheet("QPushButton{background-color:lightblue;}\
                    QPushButton:hover{background-color:lightgreen;}\
                    QPushButton:pressed{background-color:rgba(170,155,221,1);}");
//创建信号连接(第二种连接方式,不可传递参数)
connect(btnMin,&QPushButton::clicked,this,&QWidget::showNormal);

The third method:
use the new Lambda feature syntax in C++11
:

[可访问外部变量](参数表)->返回值{
    
     函数体 }    **返回值为空时可省略**
1[变量1,变量2,...](){
    
    } 指定部分变量可访问
2[=](){
    
    } 值传递方式捕获所有外部变量(内部无法改变实际变量)
3[&](){
    
    } 引用方式捕获所有外部变量(内部可以改变实际变量)

Example:

auto btnTest = new QPushButton("测试按钮",this);
btnTest->move(100,100);
//设置大小
btnTest->resize(100,30);
//设置字体
QFont font3("楷体",15,1);
btnTest->setFont(font3);
btnTest->setParent(this);
//设置样式
btnTest->setStyleSheet("QPushButton{background-color:lightblue;}\
                    QPushButton:hover{background-color:lightgreen;}\
                    QPushButton:pressed{background-color:rgba(170,155,221,1);}");
//创建信号连接(第二种连接方式,Lambda表达式)
connect(btnTest,&QPushButton::pressed,[]()->void{
    
    
    qDebug()<<"按钮被按下....";
});
connect(btnTest,&QPushButton::released,[](){
    
    
    qDebug()<<"按钮被抬起....";
}); 

3. Custom signals and slots

When customizing signals, use the keyword signals , only need to define, do not need to implement.
When customizing slot functions, use public slots , which needs to be implemented
. Example:
first define the signal and slot functions in the header file:

signals:
    void printInfo();

public slots:
    void onPrintInfo();

Then add a button to the interface. After clicking the button, a custom signal is emitted, and the custom signal is bound to the custom slot function at the same time:

auto selfBtn = new QPushButton("测试");
selfBtn->move(100,100);
//设置大小
selfBtn->resize(100,30);
//设置字体
QFont font4("楷体",15,1);
selfBtn->setFont(font4);
selfBtn->setParent(this);
//设置样式
selfBtn->setStyleSheet("QPushButton{background-color:lightblue;}\
                    QPushButton:hover{background-color:lightgreen;}\
                    QPushButton:pressed{background-color:rgba(170,155,221,1);}");
//点击按钮后,发射自定义信号
connect(selfBtn,&QPushButton::clicked,this,[=](){
    
    
    emit printInfo();
});
//将自定义的信号和自定义的槽绑定
connect(this,&Widget::printInfo,this,&Widget::onPrintInfo);

//槽函数的简单实现
void Widget::onPrintInfo()
{
    
    
    qDebug()<<"输出一些打印信息...";
}

4. Signal and slot overloading

If there are overloads for signals and slots, when connecting signals and slots, you can use function pointers to connect, and it is also possible to use the SIGNAL and SLOT keywords to connect.
First, define two overloaded signals and slots:

signals:
    void printInfo();
    void printInfo(QString mStr);

public slots:
    void onPrintInfo();
    void onPrintInfo(QString str);

Then implement the two overloaded slot functions in the source file:

void Widget::onPrintInfo()
{
    
    
    qDebug()<<"输出一些打印信息...";
}

void Widget::onPrintInfo(QString str)
{
    
    
    qDebug()<<str;
}

Finally, when connecting, you need to use the function pointer to find the specified signal and slot:

auto selfBtn = new QPushButton("测试");
selfBtn->move(btnTest->x(),btnTest->y()+50);
//设置大小
selfBtn->resize(100,30);
//设置字体
QFont font4("楷体",15,1);
selfBtn->setFont(font4);
selfBtn->setParent(this);
//设置样式
selfBtn->setStyleSheet("QPushButton{background-color:lightblue;}\
                    QPushButton:hover{background-color:lightgreen;}\
                    QPushButton:pressed{background-color:rgba(170,155,221,1);}");
connect(selfBtn,&QPushButton::clicked,this,[=](){
    
    
    emit printInfo();
    emit printInfo("带参数的信号...");
});

//定义函数指针,指向对应的信号和槽函数
void(Widget::*printSignal)() = &Widget::printInfo;
void(Widget::*printSlot)() = &Widget::onPrintInfo;
void(Widget::*printSignalStr)(QString) = &Widget::printInfo;
void(Widget::*printSlotStr)(QString) = &Widget::onPrintInfo;
//信号槽连接
connect(this,printSignal,this,printSlot);
connect(this,printSignalStr,this,printSlotStr);

5. Disconnect

After a signal is connected to multiple slot functions, if you want one of the signal slots to no longer work, you can disconnect them and use the function disconnect , which is the same as connect .

disconnect(this,&Widget::printInfo,this,&Widget::onPrintInfo);

Guess you like

Origin blog.csdn.net/FY_13781298928/article/details/130742130