QObject类r的下列函数提供了阻止、删除信号处理程序的方法:
blockSignals () - 如果Flag为True,暂时阻止接收信号;Flag为True时,恢复接收。该函数返回前面连接的状态值;
signalsBlocked () - 返回信号是否被阻止;
disconnect () - 删除信号处理程序,格式为:
. .disconnect ([])
. []. Disconnect ([])
如果没有指定,将删除所有已指定的处理程序。
例如:
button.clicked.disconnect()
button.clicked[bool].disconnect(on_clicked_button)
button.clicked["bool"].disconnect(on_clicked_button)
下面的例子中,创建了一个有四个按钮的窗口:
按下“Click me”按钮,控制台输出“Button1 pressed”;
按下“Block”按钮,再按“Click me”按钮时,控制台无输出;
按下“Unlock”按钮,再按“Click me”按钮时,控制台输出“Button1 pressed”;
按下“Delete Handler”按钮,再按“Click me”按钮时,控制台无输出;
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtWidgets
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.setWindowTitle("Blocking and Removing a Handler")
self.resize(300, 150)
self.button1 = QtWidgets.QPushButton ("Click me")
self.button2 = QtWidgets.QPushButton ("Block")
self.button3 = QtWidgets.QPushButton ("Unlock")
self.button4 = QtWidgets.QPushButton ("Delete Handler")
self.button3.setEnabled(False)
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(self.button1)
vbox.addWidget(self.button2)
vbox.addWidget(self.button3)
vbox.addWidget(self.button4)
self.setLayout(vbox)
self.button1.clicked.connect(self.on_clicked_button1)
self.button2.clicked.connect(self.on_clicked_button2)
self.button3.clicked.connect(self.on_clicked_button3)
self.button4.clicked.connect(self.on_clicked_button4)
@QtCore.pyqtSlot()
def on_clicked_button1(self):
print("Button1 pressed")
@QtCore.pyqtSlot()
def on_clicked_button2(self):
self.button1.blockSignals(True)
self.button2.setEnabled(False)
self.button3.setEnabled(True)
@QtCore.pyqtSlot()
def on_clicked_button3(self):
self.button1.blockSignals(False)
self.button2.setEnabled(True)
self.button3.setEnabled(False)
@QtCore.pyqtSlot()
def on_clicked_button4(self):
self.button1.clicked.disconnect(self.on_clicked_button1)
self.button2.setEnabled(False)
self.button3.setEnabled(False)
self.button4.setEnabled(False)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())