信号
1. signal关键字声明(不需要定义):void mysignal();
2. 无返回值
3. 可重载:void mysignal(int value);
4. 发射信号:emit mysignal();/emit mysignal(100);
槽
1. 使用slots关键字声明
2. 成员函数/静态函数、全局函数都可以作为槽函数
连接connect
1. Q_OBJECT
2. 信号和槽的参数要一致
3. 使用指针连接信号和槽函数
a. 不带参
connect(btn, &QPushButton::pressed, this, &Qmainwindow::myslot);
b. 带参(需要强转)
void (My::*mysignal))(int) = &QPushButton::pressed;
connect(btn, &QPushButton::pressed, this, &Qmainwindow::myslot);
或者
connect(btn, static_cast<void (QPushButton::*)(int)>(&QPushButton::pressed), this, &Qmainwindow::myslot);
4. 使用SIGNAL、SLOT连接信号和槽函数(不进行错误检查,如果信号或槽不存在或参数不对并不会报错)
a. SIGNAL、SLOT将函数名称转为字符串,找到对应的函数(C++编译器编译时,将函数名称和参数类型组合,作为新的函数名)
b. 此时槽函数需要slots声明
c. 带参:connect(btn, SIGNAL(pressed()), this, SLOT(myslot()));
d. 不带参:connect(btn, SIGNAL(pressed(int)), this, SLOT(myslot(int)));
5. 使用信号连接