Qt程序中有时候不希望信号槽的触发,在某段流程结束之后,又需要继续回复信号槽状态,这时候可以用阻塞或者断开信号槽的方法来处理。
- 阻塞方法:bool QObject::blockSignals(bool block)
qt描述:阻塞模式下,这个对象发送的信号都会被阻塞,解除阻塞后则不再阻塞。返回值为之前状态的阻塞情况。
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). If block is false, no such blocking will occur.
The return value is the previous value of signalsBlocked().
使用:可以参考博客:https://blog.csdn.net/aggressive_snail/article/details/51445570
void Rot13::changeLeft()
{
left->blockSignals( TRUE );
left->setText( rot13( right->text() ) );
left->blockSignals( FALSE );
}
即:在上述的过程中,设置text的signal都会阻塞
- 断开信号槽方法:bool QObject::disconnect(const QObject * sender, const char * signal, const QObject * receiver, const char * method)
qt描述:disconnect将断开sender和receiver,成功范围true
Disconnects signal in object sender from method in object receiver. Returns true if the connection is successfully broken; otherwise returns false.
有下面3中特殊用法:
a. 断开所有连接到某一对象的信号槽:(这个和阻塞的功能类似)
disconnect(myObject, 0, 0, 0);
//等价于:
myObject->disconnect();
b. 断开所有连接到某一信号的信号槽:
disconnect(myObject, SIGNAL(mySignal()), 0, 0);
// 等价于:
myObject->disconnect(SIGNAL(mySignal()));
c. 断开两个对象之间的所有信号槽关系:
disconnect(myObject, 0, myReceiver, 0);
// 等价于:
myObject->disconnect(myReceiver);
不难发现,这里的“0”就代表:所有 (any)
使用disconnect可以实现精准的信号槽断开操作,不过对于需要大范围的断开,使用阻塞还是比较方便的
转载 https://blog.csdn.net/u014339020/article/details/79670280