qt重载信号槽连接方式总结


一、qt4方式:
QObject::connect(信号发出者地址, SIGNAL(信号发出者函数名称(信号发出者函数参数...)),信号接收者地址,  SLOT(信号接收者槽函数名称(信号接收者槽函数参数...)));
注意:若在QObject子类内部定义信号槽,可直接调用connect函数。
二、qt5方式:
1、普通信号
QObject::connect(信号发出者地址,信号发出者函数名称(信号发出者函数参数...),信号接收者地址,信号接收者槽函数名称(信号接收者槽函数参数...));
注意:若在QObject子类内部定义信号槽,可直接调用connect函数。
2、重载的信号
2.1采用函数指针
void (类型:* ptrOverloadSignalFunction)(信号重载函数参数...)=&类型:: 信号;
QObject::connect(信号发出者地址,ptrOverloadSignalFunction,信号接收者地址,信号接收者槽函数名称(信号接收者槽函数参数...));
2.2采用static_cast
QObject::connect(信号发出者地址,static_cast(&类型:: 信号),信号接收者地址,信号接收者槽函数名称(信号接收者槽函数参数...));
2.3采用qOverLoad
QObject::connect(信号发出者地址,qOverLoad<信号重载函数参数...>::of(&类型:: 信号),信号接收者地址,信号接收者槽函数名称(信号接收者槽函数参数...));

三、示例

class SignalSender:public QObject
{
...
 signals:
 void overLoadSignalFunction(bool b);
 void overLoadSignalFunction(bool b,int i);
...
}

class SignalReceiver:public QObject
{
...
 public slots:
 void overLoadSignalFunctionSlot(bool b);
 void overLoadSignalFunctionSlot(bool b,int i);
...
}

void initConnections()
{

SignalSender sender;
SignalReceiver receiver;

//以下方式任取其一
//方式1:qt4
QObject::connect(sender,SIGNAL(overLoadSignalFunction(bool)),receiver,SLOT(overLoadSignalFunctionSlot(bool)));
QObject::connect(sender,SIGNAL(overLoadSignalFunction(bool,int)),receiver,SLOT(overLoadSignalFunctionSlot(bool,int)));

//方式2:qt5之函数指针
void (SignalSender::* ptrOverLoadSignalFunction1)(bool)=&SignalSender::overLoadSignalFunction;
void (SignalReceiver::* ptrOverLoadSignalFunctionSlot1)(bool)=&SignalReceiver::overLoadSignalFunction;
QObject::connect(sender,ptrOverLoadSignalFunction1,receiver,ptrOverLoadSignalFunctionSlot1);

void (SignalSender::* ptrOverLoadSignalFunction2)(bool,int)=&SignalSender::overLoadSignalFunction;
void (SignalReceiver::* ptrOverLoadSignalFunctionSlot2)(bool,int)=&SignalReceiver::overLoadSignalFunction;
QObject::connect(sender,ptrOverLoadSignalFunction2,receiver,ptrOverLoadSignalFunctionSlot2);

//方式3:qt5之static_cast
QObject::connect(sender,static_cat(&SignalSender::overLoadSignalFunction),receiver,static_cat(&SignalReceiver::overLoadSignalFunctionSlot));

QObject::connect(sender,static_cat(&SignalSender::overLoadSignalFunction),receiver,static_cat(&SignalReceiver::overLoadSignalFunctionSlot));

//方式4:qt5之qOverLoad
QObject::connect(sender,QOverload::of(&SignalSender::overLoadSignalFunction),receiver,QOverload::of(&SignalReceiver::overLoadSignalFunctionSlot));

QObject::connect(sender,QOverload::of(&SignalSender::overLoadSignalFunction),receiver,QOverload::of(&SignalReceiver::overLoadSignalFunctionSlot));

}

你可能感兴趣的:(qt)