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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理