user-interface - PyQt5 信号和插槽 'QObject has no attribute' 错误

标签 user-interface pyqt qt-signals pyqt5

我一直试图找到一种方法来从 main 之外的 Python 线程更新 GUI 线程。 PyQt5 docs在 sourceforge 上有关于如何做到这一点的很好的说明。但我仍然无法让事情发挥作用。

有没有一种很好的方法来解释交互式 session 的以下输出?难道不应该有办法在这些对象上调用发射方法吗?

>>> from PyQt5.QtCore import QObject, pyqtSignal
>>> obj = QObject()
>>> sig = pyqtSignal()
>>> obj.emit(sig)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'QObject' object has no attribute 'emit'


>>> obj.sig.emit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'QObject' object has no attribute 'sig'


>>> obj.sig = pyqtSignal()
>>> obj.sig.emit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'emit'

最佳答案

以下文字和代码在PyQt5 docs .

新信号应该只能在子类中定义 的 QObject。他们 必须是 类定义的一部分和 不能是 在定义类后动态添加为类属性。

from PyQt5.QtCore import QObject, pyqtSignal

class Foo(QObject):

    # Define a new signal called 'trigger' that has no arguments.
    trigger = pyqtSignal()

    def connect_and_emit_trigger(self):
        # Connect the trigger signal to a slot.
        self.trigger.connect(self.handle_trigger)

        # Emit the signal.
        self.trigger.emit()

    def handle_trigger(self):
        # Show that the slot has been called.

        print "trigger signal received"

关于user-interface - PyQt5 信号和插槽 'QObject has no attribute' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17578428/

相关文章:

java - 使用 ActionListener 时内部类中的静态声明非法

java - 在 Java 中使用线程

python - 如何正确关闭 PyQt 的 QtApplication?

python - 如何使用 python 和 PyQt5 将目录和文件列表从普通列表转换为树列表

python - PyQt 中支持拖放的 QTreeView

c++ - qt 信号未定义引用错误

java - 如何使用拖放交换 GridLayout 中排列的按钮上的图标?

user-interface - Blackberry - 应用程序加载屏幕

c++ - 如何在 Qt 中创建动态信号和槽?

python - 发出具有任意签名的新型 PyQt 信号