Resources: Python-GUI-PyQ5 general overview.xmind;QObject.xmind
1, destroyed(obj): this signal is sent when the object is destroyed (widget. Signal. Connect: connect signal and slot)
from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QObject Learning") # Window title self.resize(500, 500) # Set window size self.setup_ui() def setup_ui(self): self.QObject Operation of signals() def QObject Operation of signals(self): self.obj=QObject() #self means Window, which will not be released def destroy_cao(obj): #Listening for obj 2 object is released print("obj2 Object is released",obj) self.obj.destroyed.connect(destroy_cao) #There is no print here: "object2 object is released < pyqt5. Qtcore. QObject object at 0x00000204e0f7d310 >" del self.obj #Here print "object 2 is released < pyqt5. Qtcore. QObject object at 0x00000204e0f7d310 >" if __name__ == '__main__': import sys app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
2, Objectname changed (objectname): this signal is emitted when the object name changes
from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QObject Learning") # Window title self.resize(500, 500) # Set window size self.setup_ui() def setup_ui(self): self.QObject Operation of signals() def QObject Operation of signals(self): self.obj=QObject() def obj_name_cao(name): print("Object name changed",name) self.obj.objectNameChanged.connect(obj_name_cao) self.obj.setObjectName("666") if __name__ == '__main__': import sys app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) #Return ''' //Object name changed 666 '''
3, obj.disconnect(): disconnect signal from slot.
obj: control > cancel all signal connections of the control, signal > cancel the connection of the specified signal
from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QObject Learning") # Window title self.resize(500, 500) # Set window size self.setup_ui() def setup_ui(self): self.QObject Operation of signals() def QObject Operation of signals(self): self.obj=QObject() def obj_name_cao(name): print("Object name changed",name) self.obj.objectNameChanged.connect(obj_name_cao) self.obj.setObjectName("666") self.obj.objectNameChanged.disconnect(obj_name_cao) #Although the signal is triggered, the signal is disconnected from the slot self.obj.setObjectName("888") if __name__ == '__main__': import sys app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) #Return ''' //Object name changed 666 '''
4, widget.blockSignals(bool): temporarily (cancel) the connection between all signals of the control and the slot
from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QObject Learning") # Window title self.resize(500, 500) # Set window size self.setup_ui() def setup_ui(self): self.QObject Operation of signals() def QObject Operation of signals(self): self.obj=QObject() def obj_name_cao(name): print("Object name changed",name) self.obj.objectNameChanged.connect(obj_name_cao) self.obj.setObjectName("666") self.obj.blockSignals(True) #Temporary block self.obj.setObjectName("888") self.obj.blockSignals(False) self.obj.setObjectName("777") if __name__ == '__main__': import sys app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) #Return ''' //Object name changed 666 //Object name changed 777 '''
5, widget.signalsBlocked(): whether the signal is blocked
from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QObject Learning") # Window title self.resize(500, 500) # Set window size self.setup_ui() def setup_ui(self): self.QObject Operation of signals() def QObject Operation of signals(self): self.obj=QObject() def obj_name_cao(name): print("Object name changed",name) self.obj.objectNameChanged.connect(obj_name_cao) self.obj.setObjectName("666") print(self.obj.signalsBlocked(),"1") self.obj.blockSignals(True) #Temporary block print(self.obj.signalsBlocked(),"2") self.obj.setObjectName("888") self.obj.blockSignals(False) #Reply connection print(self.obj.signalsBlocked(),"3") self.obj.setObjectName("777") if __name__ == '__main__': import sys app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) #Return ''' //Object name changed 666 False 1 True 2 False 3 //Object name changed 777 '''
6, Widget. Receivers: returns the number of receivers connected to the signal
from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QObject Learning") # Window title self.resize(500, 500) # Set window size self.setup_ui() def setup_ui(self): self.QObject Operation of signals() def QObject Operation of signals(self): self.obj=QObject() def obj_name_cao(name): print("Object name changed 1",name) def obj_name_cao2(name): print("Object name changed 2",name) self.obj.objectNameChanged.connect(obj_name_cao) self.obj.objectNameChanged.connect(obj_name_cao2) print(self.obj.receivers(self.obj.objectNameCAhanged)) self.obj.setObjectName("666") if __name__ == '__main__': import sys app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) #Return ''' 2 //Object name changed 1 666 //Object name changed 666 '''