pyside2&pyqt5的信号与槽机制

1、信号与槽的两种写法

第一种情况:

from PySide2 import QtWidgets, QtCore
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
btn = QtWidgets.QPushButton(widget)
btn.clicked.connect(widget.close)
widget.show()
sys.exit(app.exec_())

第二种情况:

from PySide2 import QtWidgets, QtCore
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
btn = QtWidgets.QPushButton(widget)
QtCore.QObject.connect(btn,QtCore.SIGNAL("clicked()"),widget,QtCore.SLOT("close()"))
widget.show()
sys.exit(app.exec_())

2、自定义槽函数

from PySide2 import QtWidgets, QtCore
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
btn = QtWidgets.QPushButton(widget)
@QtCore.Slot()
def changeTex():
btn.setText("ok")
btn.clicked.connect(changeTex)
widget.show()
sys.exit(app.exec_())

3、带参数槽函数

(1)lambad写法:

from PySide2 import QtWidgets,QtCore
import sys
class MyForm(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
button1 = QtWidgets.QPushButton(self)
button1.clicked.connect(lambda: self.on_button(1))
def on_button(self, n):
print('Button {0} clicked'.format(n)) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()

(2)使用functools里的partial函数。

from functools import partial
from PySide2 import QtWidgets,QtCore
import sys
class MyForm(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
button1 = QtWidgets.QPushButton(self)
button1.clicked.connect(partial(self.on_button, 1))
def on_button(self, n):
print('Button {0} clicked'.format(n)) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()

最新文章

  1. local认证
  2. 内存对齐 和 sizeof小结
  3. xenomai for at91
  4. nodejs入门
  5. 利用注解进行sql反射代码示例
  6. 69. Letter Combinations of a Phone Number
  7. HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
  8. Asynchronous
  9. poj 1573Robot Motion
  10. 二 Djano模型层之模型字段选项
  11. Jmeter 性能测试术语
  12. Android jni c/c++线程通过CallVoidMethod调用java函数出现奔溃问题
  13. MogileFS-2.44 安装与配置
  14. Material Design Support 8大控件介绍
  15. 找不到reportviewer控件在哪儿
  16. 转码:gcc在代码中禁止某些warning
  17. Linux调度器 - deadline调度器
  18. 如何理解Latency和Throughput: 吞吐量和延迟
  19. this指向 - Node环境
  20. 【Qt开发】QTime类

热门文章

  1. drush use dev.mentor.com | expecting statement
  2. clickhouse杂记
  3. wsl无法创建文件与修改文件
  4. ubuntu 16.04 安装peach
  5. Mac下Virtual Box 6.1 Host-Only 网络配置 没有虚拟网卡
  6. hierarchical-clustering
  7. Bug_Android error: duplicate attribute. (错误:重复属性。)
  8. SQLSERVER 根据一个库的视图在另一个库中生成一张表
  9. js实现不同的域名 输出不同的结果
  10. 【SQL Server】numeric——精确数字的数据类型