网络上搜索,大部分都废话连篇,直接上码测试此功能,结果显示OK。
分别创建三个类,A,B,C。信号和槽绑定关系如下,一个信号绑定两个槽函数:
A::A(QObject *parent) : QObject(parent)
{
B* b = new B();
C* c = new C();
connect(this, SIGNAL(Greet()), b, SLOT(Response()));
connect(this, SIGNAL(Greet()), c, SLOT(Response()));
}
void A::SayHello()
{
emit Greet();
}
两个槽函数:
void B::Response()
{
qDebug()<<"B response";
}
void C::Response()
{
qDebug()<<"C response";
}
主函数调用:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
A* ca = new A();
ca->SayHello();
return a.exec();
}
运行结果:
B response
C response
原文链接: https://blog.csdn.net/mz5111089/article/details/79383056
离线