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

原文地址:https://www.cnblogs.com/neo3301/p/13169862.html