资源:Python-GUI-PyQ5总概述.xmind;QObject.xmind
一、destroyed(obj):对象呗销毁时,发射此信号(其中widget.信号.connect(槽):连接信号与槽)
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习") # 窗口标题
self.resize(500, 500) # 设置窗口大小
self.setup_ui()
def setup_ui(self):
self.QObject信号的操作()
def QObject信号的操作(self):
self.obj=QObject() #self是指Window,不会被释放
def destroy_cao(obj): #监听obj2对象被释放
print("obj2对象被释放了",obj)
self.obj.destroyed.connect(destroy_cao) #这里并没有打印"obj2对象被释放了 <PyQt5.QtCore.QObject object at 0x00000204E0F7D310>"
del self.obj #这里打印"obj2对象被释放了 <PyQt5.QtCore.QObject object at 0x00000204E0F7D310>"
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
二、obj.objectNameChanged(objectName):对象名称改变时发射此信号
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习") # 窗口标题
self.resize(500, 500) # 设置窗口大小
self.setup_ui()
def setup_ui(self):
self.QObject信号的操作()
def QObject信号的操作(self):
self.obj=QObject()
def obj_name_cao(name):
print("对象名称发生了改变",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_())
#返回
'''
对象名称发生了改变 666
'''
三、obj.disconnect():取消连接信号与槽。
obj:控件-->取消控件的所有信号连接,信号-->取消指定信号的连接
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习") # 窗口标题
self.resize(500, 500) # 设置窗口大小
self.setup_ui()
def setup_ui(self):
self.QObject信号的操作()
def QObject信号的操作(self):
self.obj=QObject()
def obj_name_cao(name):
print("对象名称发生了改变",name)
self.obj.objectNameChanged.connect(obj_name_cao)
self.obj.setObjectName("666")
self.obj.objectNameChanged.disconnect(obj_name_cao) #虽然信号被触发,但是信号与槽的关系断开
self.obj.setObjectName("888")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
#返回
'''
对象名称发生了改变 666
'''
四、widget.blockSignals(bool):临时(取消)指定控件所有的信号与槽的连接
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习") # 窗口标题
self.resize(500, 500) # 设置窗口大小
self.setup_ui()
def setup_ui(self):
self.QObject信号的操作()
def QObject信号的操作(self):
self.obj=QObject()
def obj_name_cao(name):
print("对象名称发生了改变",name)
self.obj.objectNameChanged.connect(obj_name_cao)
self.obj.setObjectName("666")
self.obj.blockSignals(True) #临时阻断
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_())
#返回
'''
对象名称发生了改变 666
对象名称发生了改变 777
'''
五、widget.signalsBlocked():信号是否被阻止
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习") # 窗口标题
self.resize(500, 500) # 设置窗口大小
self.setup_ui()
def setup_ui(self):
self.QObject信号的操作()
def QObject信号的操作(self):
self.obj=QObject()
def obj_name_cao(name):
print("对象名称发生了改变",name)
self.obj.objectNameChanged.connect(obj_name_cao)
self.obj.setObjectName("666")
print(self.obj.signalsBlocked(),"1")
self.obj.blockSignals(True) #临时阻断
print(self.obj.signalsBlocked(),"2")
self.obj.setObjectName("888")
self.obj.blockSignals(False) #回复连接
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_())
#返回
'''
对象名称发生了改变 666
False 1
True 2
False 3
对象名称发生了改变 777
'''
六、widget.receivers(信号):返回连接到信号的接收器数量
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject的学习") # 窗口标题
self.resize(500, 500) # 设置窗口大小
self.setup_ui()
def setup_ui(self):
self.QObject信号的操作()
def QObject信号的操作(self):
self.obj=QObject()
def obj_name_cao(name):
print("对象名称发生改变1",name)
def obj_name_cao2(name):
print("对象名称发生改变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_())
#返回
'''
2
对象名称发生改变1 666
对象名称发生改变2 666
'''