• 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

  • 相关阅读:
    [转载]Dorado学习笔记
    [转载]dorado学习笔记(二)
    [转载]Struts+Spring+HibernateSSH整合实例
    [转载]Dorado中DataTable使用技巧汇总
    [转载]Struts+Spring+HibernateSSH整合实例
    JAVA基础知识复习面试笔试宝典
    [转载]Dorado中DataTable使用技巧汇总
    [转载]Dorado学习笔记
    IE网页中PNG格式图片无法显示的解决方法
    网络字节序和主机字节序
  • 原文地址:https://www.cnblogs.com/neo3301/p/13169862.html
Copyright © 2020-2023  润新知