pyqt 使用问题总结

环境:py2.7.14 + pyqt4

1.在使用下列方式创建信号连接时

self.connect(button, QtCore.SIGNAL('clicked()'),self.slot1(arg))

报错提示为:

TypeError: arguments did not match any overloaded call:
  QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'


解析:

这里的slot1()是一个函数,当槽函数是自定义的函数时要这样,用lambda: self.slot1(arg)替换self.slot1(arg)即可

或者这个slot1只是一个method,不需要参数时,用slot1替换slot即可。

看到这,其本质是python调用函数时加不加括号的区别。

def ab():
    return  1+2
print ab
print ab()

结果:

<function ab at 0x00000000035AB748>
3

综上,其区别在与不加括号时,调用的时这个函数的,这个对象的内存地址,

加括号时你调用的是这个函数的运行结果

参考StackOverflow:

person a:The connect() method expects a callable argument. When you write self.Soft_Memory() you are making a call to that method, and the result of that call (None, since you don't explicitly return anything) is what is being passed to connect().

person b:You want to pass a reference to the method itself.

you should use a reference of the method, instead of calling it:

self.PB1.clicked.connect(self.Soft_Memory)

However, you might often need to pass arguments on those functions (I certainly do). On those situations, if you need to use args there's a workaround by using lambda.

self.PB1.clicked.connect(lambda: myfunction(self, arg1, True, "example", arg4))

参考链接:

https://stackoverflow.com/questions/45793966/clicked-connect-error

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值