【问题标题】:PyQt5 in what module is the emit method found?PyQt5 在哪个模块中找到了 emit 方法?
【发布时间】:2013-07-09 00:34:48
【问题描述】:

如果有人为我运行此代码作为健全性检查会很有帮助。

Python 3.3.1 (default, Apr 17 2013, 22:30:32) 
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>from PyQt5.QtCore import pyqtSignal
>>>for i in dir(pyqtSignal):
...    if i == 'emit':
...         print(True)
...
>>>

为其他人返回 true 吗?请注意,从 PyQt4 导入 QObject:

>>> from PyQt4.QtCore import QObject
>>> for i in dir(QObject):
...     if i == 'emit':
...             print(True)
... 
True

【问题讨论】:

    标签: pyqt qt-signals pyqt5


    【解决方案1】:

    pyqtSignal 不是信号,它是用于创建信号的工厂函数,所以它当然没有emit 属性。它只返回一个descriptor,当绑定到一个 QObject 实例时将返回实际的信号对象。这意味着只有 bound 信号会有emit 方法。

    QObject.emit 方法是 pyqt 中引入新样式信号之前的遗留物,现在是 has been removed。只需在绑定信号上使用emit 方法即可发出它:

    class SomeObject(QObject):
        someSignal = pyqtSignal(...)
    
    instance = SomeObject()
    instance.someSignal.emit(value)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      相关资源
      最近更新 更多