QT中使用信号与槽机制来传递信号,实现按钮响应,菜单栏响应等操作。与先实例化类的对象,再调用类的成员函数,实现了类似的功能。
本文讨论QT中使用信号与槽机制来传递信号的2中情况:
(1)类中信号与槽连接
connect(ui->action_Open, SIGNAL(triggered()), this, SLOT(showOpenFileDlg()));
(2)类之间槽的连接
//类间信号与槽连接,但是没有传递数据
geometryTransform* geomtry = new geometryTransform();//实例化类的对象
connect(ui->action_Scale, SIGNAL(triggered()), geomtry, SLOT(scale()));
//类间信号与槽连接,并且传递数据
imageEnhance* imgEnhance = = new imageEnhance();//实例化类的对象
//A->B->A,A 向B发送信号,B执行处理,处理结果再返回A显示,即操作与显示分开,更合理
connect(ui->action_MediumFilter, SIGNAL(triggered()), this, SLOT(sendQPixmap()));
connect(this,SIGNAL(getQPixmap(QString)), imgEnhance, SLOT(meanFilter(QString)));
connect(imgEnhance, SIGNAL(getQpixmap(QPixmap*)), this,SLOT(updateView(QPixmap*)));