自定义信号的声明 [英] Declaration of the custom Signals

查看:61
本文介绍了自定义信号的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Qt 中,我们可以通过使它们成为静态变量来创建自定义信号.然后我们使用 self.signame 代替 classname.signame.这样就在类中创建了一个实例变量.

In Qt, we can create custom signals by making them static variables. and then we use self.signame instead classname.signame. So that creates an instance variable in the class.

我想知道这种模式之外的理论.

I wanted to know the theory beyond this pattern.

这是我尝试过的一些伪代码,它们记录在大多数来源中:

here's some pseudo-code that I have tried that was recorded in most of the sources:

from PyQt5 import QtWidgets,QtCore
class test(QtWidgets.QApplication):
    sig=QtCore.pyqtSignal([bool])
    def __init__(self):
        super().__init__([])
        # self.sig=QtCore.pyqtSignal([bool]) #1
        self.window=QtWidgets.QWidget()
        self.sig.connect(lambda x=True:print('Hello World'))
        self.bt=QtWidgets.QPushButton(self.window,text='test',clicked=self.sig)
        self.window.setLayout(QtWidgets.QVBoxLayout())
        self.window.layout().addWidget(self.bt)
        self.window.show()
test().exec()

当我尝试访问信号 test.sig 而不是 self.sig 时,

When i tred to access the signal test.sig instead of self.sig ,

from PyQt5 import QtWidgets,QtCore
class test(QtWidgets.QApplication):
    def __init__(self):
        super().__init__([])
        self.sig=QtCore.pyqtSignal([bool])
        self.window=QtWidgets.QWidget()
        self.sig.connect(lambda x=True:print('Hello World'))
        self.bt=QtWidgets.QPushButton(self.window,text='test',clicked=self.sig)
        self.window.setLayout(QtWidgets.QVBoxLayout())
        self.window.layout().addWidget(self.bt)
        self.window.show()
test().exec()

我收到此错误:

    test.sig.connect(lambda x=True:print('Hello World'))
AttributeError: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'

当我尝试将信号作为实例变量而不是静态变量时,我得到了那个错误(在伪代码中取消注释 #1)

and when I tried to make the signal as an instance variable instead of the static variable I get that error (uncommenting the #1 in psudeo code)

我遇到了同样的错误(和以前一样).

I get the same error (as before).

我能知道这背后的原因吗

May I know the reason behind this

来源:PyQt5

推荐答案

需要注意的是 pyqtSignal 被声明为类的属性,但它与 文档:

It should be noted that pyqtSignal is declared as an attribute of the class but it is not the same object that is used in the connection as indicated in the docs:

信号(特别是未绑定的信号)是一个类属性.当一个信号被引用为类的实例的属性然后PyQt5 自动将实例绑定到信号以便创建绑定信号.这与 Python 本身的机制相同用于从类函数创建绑定方法.

A signal (specifically an unbound signal) is a class attribute. When a signal is referenced as an attribute of an instance of the class then PyQt5 automatically binds the instance to the signal in order to create a bound signal. This is the same mechanism that Python itself uses to create bound methods from class functions.

绑定信号具有connect()、disconnect()和emit()方法实现相关的功能.它也有一个信号属性这是 Qt 返回的信号的签名SIGNAL() 宏.

A bound signal has connect(), disconnect() and emit() methods that implement the associated functionality. It also has a signal attribute that is the signature of the signal that would be returned by Qt’s SIGNAL() macro.

换句话说,sig = QtCore.pyqtSignal([bool]) 是未绑定信号,但 self.sig 是绑定信号,可以使用以下几行:

In other words, sig = QtCore.pyqtSignal([bool]) is an unbound signal but self.sig is the bound signal and that can be verified with the following lines:

from PyQt5 import QtWidgets, QtCore


class test(QtWidgets.QApplication):
    sig = QtCore.pyqtSignal([bool])

    print(type(sig))

    def __init__(self):
        super().__init__([])
        self.window = QtWidgets.QWidget()
        print(type(self.sig))
        self.sig.connect(lambda x=True: print("Hello World"))
        self.bt = QtWidgets.QPushButton(self.window, text="test", clicked=self.sig)
        self.window.setLayout(QtWidgets.QVBoxLayout())
        self.window.layout().addWidget(self.bt)
        self.window.show()


test().exec()

输出

<class 'PyQt5.QtCore.pyqtSignal'>
<class 'PyQt5.QtCore.pyqtBoundSignal'>

总而言之,类sig"的属性是是一个 pyqtSignal,它没有 connect 方法,用于构造属性self.x".这是一个具有 connect 方法的 pyqtBoundSignal.

In conclusion, the attribute of the class "sig" is a pyqtSignal that does not have the connect method and that is used to construct the attribute "self.x" which is a pyqtBoundSignal that does have the connect method.

这篇关于自定义信号的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆