控件内置的一些函数,
按下按钮 QPushButton().pressed
按下并弹起按钮 QPushButton().clicked;
也可以自定义pyqtSignal()
控件内置的槽函数;也有自定义的函数/方法
对象.信号.connect(槽函数)
QObject对象信号处理API
widget.信号.connect(槽) # 连接信号和槽
obj.disconnect() # 取消连接信号与槽,无法恢复,只能重新连接
(控件.disconnect() # 取消控件的所有信号连接)
(对象.信号.disconnect() # 取消对象指定信号的连接)
widget.blockSignals(bool) # 临时(取消)阻止指定控件所有的信号与槽的连接(True:取消,False:恢复)
widget.signalsBlocked() # 判断信号是否被阻止
widget.receivers(widget.信号) # 返回当前对象指定信号连接的槽函数数量
QObject.objectNameChanged(objectName) # 对象名称发生改变时,发射此信号
QObject.destroyed(obj) # 对象被销毁时, 发射此信号
QWidget.windowTitleChanged # 窗口标题发生改变时,发射此信号
from PyQt5.Qt import *
import sys
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QObject信号与槽')
self.resize(980, 500)
self.widget_list()
def widget_list(self):
self.add_widget()
def add_widget(self):
obj = QObject()
def destroyed_cao():
print('对象被释放了!')
obj.destroyed.connect(destroyed_cao)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Windows()
w.show()
sys.exit(app.exec_())
def add_widget(self):
self.obj = QObject()
def destroyed_cao():
print('对象被释放了!')
self.obj.destroyed.connect(destroyed_cao)
print('obj对象还未销毁')
print('销毁obj对象')
del self.obj
print('obj对象被销毁了')
def add_widget(self):
self.obj = QObject()
def destroyed_cao(obj):
print(obj, '对象被释放了!')
self.obj.destroyed.connect(destroyed_cao)
print('obj对象还未销毁')
print('销毁obj对象')
del self.obj
print('obj对象被销毁了')
from PyQt5.Qt import *
import sys
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QObject信号与槽')
self.resize(980, 500)
self.widget_list()
def widget_list(self):
self.add_widget()
def obj_name_cao(self):
print('对象名称被改变了!')
def add_widget(self):
obj = QObject()
obj.objectNameChanged.connect(self.obj_name_cao)
print('对象名称还未修改')
print('修改对象名称')
obj.setObjectName('new_objname')
print('对象名称已被修改结束')
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Windows()
w.show()
sys.exit(app.exec_())
def obj_name_cao(self, newname):
print('对象名称被改变了!', newname)
def add_widget(self):
obj = QObject()
obj.objectNameChanged.connect(self.obj_name_cao)
print('对象名称还未修改')
print('修改对象名称')
obj.setObjectName('new_objname')
print('对象名称已被修改结束')
控件.disconnect() # 取消控件的所有信号与槽的连接
对象.信号.disconnect() # 取消对象指定信号与槽的连接
from PyQt5.Qt import *
import sys
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QObject信号与槽')
self.resize(980, 500)
self.widget_list()
def widget_list(self):
self.add_widget()
def obj_name_cao(self, newname):
print('对象名称被改变了!', newname)
def obj_name_cao2(self, newname):
print('对象名称被改变成:!', newname)
def destroyed_cao(self):
print('对象被释放了')
def add_widget(self):
obj = QObject()
obj.objectNameChanged.connect(self.obj_name_cao)
obj.objectNameChanged.connect(self.obj_name_cao2)
obj.destroyed.connect(self.destroyed_cao)
# 取消对象所有信号与槽的连接,无法恢复,只能重新连接
obj.disconnect()
# 取消对象指定信号与槽的连接
# obj.objectNameChanged.disconnect()
obj.setObjectName('修改后的对象名')
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Windows()
w.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QObject信号与槽')
self.resize(980, 500)
self.widget_list()
def widget_list(self):
self.add_widget()
def obj_name_cao(self, newname):
print('对象名称被改变了!', newname)
def obj_name_cao2(self, newname):
print('对象名称被改变成:!', newname)
def destroyed_cao(self):
print('对象被释放了')
def add_widget(self):
obj = QObject()
obj.objectNameChanged.connect(self.obj_name_cao)
obj.objectNameChanged.connect(self.obj_name_cao2)
obj.destroyed.connect(self.destroyed_cao)
# 取消对象所有信号与槽的连接,无法恢复,只能重新连接
# obj.disconnect()
# 取消对象指定信号与槽的连接
obj.objectNameChanged.disconnect()
obj.setObjectName('修改后的对象名')
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Windows()
w.show()
sys.exit(app.exec_())
控件.disconnect
是取消当前控件所有信号与槽的连接对象.信号.disconnect
是取消当前对象指定信号与槽的连接widget.blockSignals(True) # 临时取消信号与槽的连接
widget.blockSignals(False) # 恢复信号与槽的连接
from PyQt5.Qt import *
import sys
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QObject信号与槽')
self.resize(980, 500)
self.widget_list()
def widget_list(self):
self.add_widget()
def obj_name_cao(self, newname):
print('对象名称被改变了!', newname)
def add_widget(self):
obj = QObject()
obj.objectNameChanged.connect(self.obj_name_cao)
# 临时取消信号与槽的连接
obj.blockSignals(True)
obj.setObjectName('第一次修改对象名称')
# 恢复信号与槽的连接
obj.blockSignals(False)
obj.setObjectName('第二次修改对象名称')
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Windows()
w.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QObject信号与槽')
self.resize(980, 500)
self.widget_list()
def widget_list(self):
self.add_widget()
def obj_name_cao(self, newname):
print('对象名称被改变了!', newname)
def add_widget(self):
obj = QObject()
obj.objectNameChanged.connect(self.obj_name_cao)
# 临时取消信号与槽的连接
obj.blockSignals(True)
obj.setObjectName('第一次修改对象名称')
# 判断obj对象信号与槽连接是否被阻止
print('1', obj.signalsBlocked())
# 恢复信号与槽的连接
obj.blockSignals(False)
obj.setObjectName('第二次修改对象名称')
# 判断obj对象信号与槽连接是否被阻止
print('2', obj.signalsBlocked())
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Windows()
w.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QObject信号与槽')
self.resize(980, 500)
self.widget_list()
def widget_list(self):
self.add_widget()
def obj_name_cao(self, newname):
print('对象名称被改变了!', newname)
def obj_name_cao2(self, newname):
print('对象名称被改变成:!', newname)
def add_widget(self):
obj = QObject()
obj.objectNameChanged.connect(self.obj_name_cao)
obj.objectNameChanged.connect(self.obj_name_cao2)
print('开始位置:', obj.receivers(obj.objectNameChanged))
# 临时(取消)阻止该对象的所有信号与槽的连接
obj.blockSignals(True)
print('blockSignals之后:', obj.receivers(obj.objectNameChanged))
# 断开该对象所有信号与槽的连接
obj.disconnect()
print('disconnect之后:', obj.receivers(obj.objectNameChanged))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Windows()
w.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('')
self.resize(980, 500)
self.widget_list()
def widget_list(self):
self.add_widget()
def btn_cao(self):
print('点我嘎哈?')
def add_widget(self):
btn = QPushButton(self)
btn.setText("点击我")
btn.clicked.connect(self.btn_cao)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Windows()
w.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class Windows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('')
self.resize(980, 500)
self.widget_list()
def widget_list(self):
self.add_widget()
def windowtitle_cao(self, title):
# 临时取消信号与槽的连接,防止下面修改标题进入死循环
self.blockSignals(True)
self.setWindowTitle('新标题 - ' + title)
# 恢复信号与槽的连接,为下一次修改标题做准备
self.blockSignals(False)
def add_widget(self):
self.windowTitleChanged.connect(self.windowtitle_cao)
self.setWindowTitle('Hello')
self.setWindowTitle('World')
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Windows()
w.show()
sys.exit(app.exec_())