python - PyQt5 对不从 QObject 继承的类中的信号的支持

标签 python qt pyqt5 qobject

在“PyQt4 和 PyQt5 之间的差异”部分( http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html )中,我可以阅读以下行:

Unlike PyQt4, PyQt5 supports the definition of properties, signals and slots in classes not sub-classed from QObject (i.e. in mixins).

但是,在“信号和槽的支持”部分 ( http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html ),我可以读到:

New signals should only be defined in sub-classes of QObject. They must be part of the class definition and cannot be dynamically added as class attributes after the class has been defined.

我是否误解了什么或者这两个句子相互矛盾?

最佳答案

注意in mixins部分。这意味着您可以执行以下操作:

from PyQt5.QtCore import pyqtSignal, QObject

class SignalMixin:
    sig = pyqtSignal()

class Obj(SignalMixin, QObject):
    pass

o = Obj()
o.sig.connect(lambda: print('foo'))
o.sig.emit()

关于python - PyQt5 对不从 QObject 继承的类中的信号的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43634878/

相关文章:

python - 无法从源代码构建 qtermwidget

python - Pandas 数据框到以行索引为值的字典?

python - 仅递归调用internal_dep中存在的键值或跳过跳过列表中存在的键值

python - Django raw sql/postgres 时区混淆

c++ - 如何删除我提供透明背景图像的 QDialog 的边框?

macos - 在 MacOS X 下使用 Qt 在前台启动应用程序

Python如何从QDateTimeEdit中提取字符串

python - 在python中发送utf-8地址到urlretrieve

c++ - 在 Linux 中使用 Qt 的 Close() 文件描述符

python - PyQt5:QPushButton 双击?