1. 程式人生 > >Qt的訊號與槽你可能不知道的那些

Qt的訊號與槽你可能不知道的那些

說到訊號與槽,這是Qt獨有的特點。

1、應該知道的:
一般用訊號和槽都會用到:signals和slots
Qt4用法:
    connect(sender, SIGNAL(signal), receiver, SLOT(slot));
Qt5用法:
    connect(sender, &Sender::signal, receiver, &Receiver::slot);


 
2、可能不知道的:

(1)它們的巨集:Q_SIGNALS和Q_SLOTS
(2)斷開所有連線到該物件的訊號:myObject->disconnect();
(3)斷開一切連線到特定訊號:myObject->disconnect(SIGNAL(mySignal()));
(4)斷開一個特定的接收者:myObject->disconnect(myReceiver);
(5)slots的lamda用法:connect(a,&A::signal,[=](){qDebug() << "ok" ;});

(6)QTimer::singleShot(100,[=]{qDebug() << "Perfect!"; });

例:
斷開連結:disconnect(widget); //斷開widget的所有連線

訊號與槽的Lambda表示式方法比較常用,要學會喲