pyside2 镜像安装_PySide2信号与槽连接新语法注意地方

本文对比了PySide2中旧的SIGNAL()和SLOT()宏与新的Python风格的信号连接方式。旧语法使用宏进行信号和槽的绑定,而新语法直接使用对象的方法调用。重点强调新语法中槽函数不应带有括号,否则会导致槽函数在连接时立即执行。示例代码展示了正确和错误的槽函数连接方式。
摘要由CSDN通过智能技术生成

旧语法

旧语法采用了SIGNAL ()和SLOT()宏

import sys

from PySide2.QtWidgets import QApplication, QPushButton

from PySide2.QtCore import SIGNAL, QObject

def func():

print("func has been called!")

app = QApplication(sys.argv)

button = QPushButton("Call func")

QObject.connect(button, SIGNAL ('clicked()'), func)

button.show()

sys.exit(app.exec_())

新语法

新语法采用了python的风格

import sys

from PySide2.QtWidgets import QApplication, QPushButton

def func():

print("func has been called!")

app = QApplication(sys.argv)

button = QPushButton("Call func")

button.clicked.connect(func)

button.show()

sys.exit(app.exec_())

新语法注意地方

把上面的例子写成这样,也是可以过的

import sys

from PySide2.QtWidgets import QApplication, QPushButton

def func():

print("func has been called!")

app = QApplication(sys.argv)

button = QPushButton("Call func")

button.clicked.connect(func())

button.show()

sys.exit(app.exec_())

但是你会发现,程序启动的时候就触发了一次按键的单击信号,启动后再怎么点击按键都不会再执行槽函数。

问题就在button.clicked.connect(func()),槽函数不要带()!!!!!!旧语法使用了SIGNAL ()和SLOT()宏的时候是需要的,但是不用宏的时候不要带!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值