一、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
class signalreceiver:public qobject
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訊號與槽的連線方式
一.qt autoconnection qt autoconnection表示系統自動選擇相應的連線方式,如果訊號與槽在同一執行緒,就採用qt directconnection,如果訊號與槽不在同一執行緒,將採用qt queuedconnection的連線方式。二.qt directconnecti...
Qt訊號與槽連線
connect pointer1,pointer2,pointer3,pointer4 pointer1 指向傳送訊號的物件的指標 pointer2 傳送訊號的物件所對應的類的成員函式的指標 pointer3 接收訊號的物件的指標 pointer4 接收訊號的物件所對應物件的槽函式指標 總結下來就是...
QT訊號和槽總結
自定義定義乙個訊號 signals void 訊號名 引數 void click qstring 注意 signals前面不能有訪問修飾符 槽的實現 關鍵字 slots,可以被訪問修飾符修飾 private slots void clickfinished qstring connect sende...