basic definition
The so-called signal slot is actually the observer mode. When an event occurs , for example, a button detects that it has been clicked, it emits a signal . This kind of sending has no purpose and is similar to broadcasting. If an object is interested in this signal, it will use the connect function , which means that it will bind the signal it wants to process to one of its own functions (called a slot) to process the signal . That is to say, when the signal is emitted, the connected slot function will be automatically called back . This is similar to the observer pattern: when an event of interest occurs, an operation will be automatically triggered.
Standard signals and slots
First, the wizard creates a project
implemented in the constructor
Implement signals and slots
connect(&b1, &QPushButton::pressed, this, &MainWidget::close);
/* &b1: 信号发出者,指针类型
* &QPushButton::pressed:处理的信号, &发送者的类名::信号名字
* this: 信号接收者
* &MainWidget::close: 槽函数,信号处理函数 &接收的类名::槽函数名字
*/
Custom slot
/* 自定义槽,普通函数的用法
* Qt5:任意的成员函数,普通全局函数,静态函数
* 槽函数需要和信号一致(参数,返回值)
* 由于信号都是没有返回值,所以,槽函数一定没有返回值
*/
connect(b2, &QPushButton::released, this, &MainWidget::mySlot);
connect(b2, &QPushButton::released, &b1, &QPushButton::hide);
/* 自定义的槽 在头文件mainwidget.h中 声明 */
void MainWidget::mySlot()
{
b2->setText("123");
}
Custom signal to achieve two independent window conversions
Create new subwindow
Customize signal and slot functions and implement them