父窗口直接调用函数触发子窗口信号的情况见本文
父窗口调用信号和槽函数 触发子窗口信号的情况参见该文:
本文中,我们将使用QPushbutton按钮clicked 之后响应的槽函数改为父窗口的槽函数,而不是子窗口的槽函数
1、A类中会发射信号(子部件)
class CA{
signals:
void ASignal(quint8 pare_1,quint8 pare_2); //信号只有声明,没有定义,只是简单的传递函数
//slots:
// void sendfunc(); //这个函数定义为槽函数
void sendfunc(); //子窗口函数改为普通函数
};
void CA::sendfunc(){ //A中的某个函数会发射这个信号
//do sh.
emit ASignal(100,200); //发射信号
}
2、B类中接收信号(父窗口)
class CB{
CA m_send; //CA类对象 //父窗口中会定义一个子部件类对象,就如同窗口中会new QPushbutton;
slots:
void reponsefunc(quint8 pare_1,quint8 pare_2); //槽函数
void reponse();
};
class CB::CB(){
//do sth.
//将对子窗口的操作发出信号,在父窗口中响应。
connect(&m_send,SIGNAL(ASignal(quint8 ,quint8)),this,SLOT(respnsefunc(qint8,qint8)));//将类A中的信号与类B中响应关联
//触发子窗口信号方式1
//m_send.sendfunc();
//触发子窗口信号方式2 //父窗口中通过点击按钮来触发子窗口信号,子窗口信号触发后,父窗口会响应该信号。
//connect(ui->pushbutton,SIGNAL(clicked()),&m_send,SLOT(sendfunc()));
connect(ui->pushbutton,SIGNAL(clicked()),this,SLOT(reponse());
}
void class CB::reponsefunc(quint8 pare_1,quint8 pare_2){
//do sth.
}
void class CB::reponse(){
m_send.sendfunc(); //添加在这里,子窗口触发信号
}
总结: connect(&m_send,SIGNAL(ASignal(quint8 ,quint8)),this,SLOT(respnsefunc(qint8,qint8)));
信号与槽的实质是两个类 中参数的传递,以上为关键代码。