简体   繁体   English

有什么方法可以直接在 PySide2 中将自定义 Signal(int) 与 Push-Button 连接起来?

[英]Is there any way to connect custom Signal(int) with Push-Button directly in PySide2?

I want to connect a trigger generated from PushButton to my custom signal with an argument(Signal(int)).我想使用参数(Signal(int))将 PushButton 生成的触发器连接到我的自定义信号。

It is possible to connect a signal without an argument to the Button.可以将不带参数的信号连接到 Button。 Currently, I am creating an extra slot to emit the Signal(int) triggered from Signal() and Button.目前,我正在创建一个额外的插槽来发出由 Signal() 和 Button 触发的 Signal(int)。 Is there any simple way to this?有什么简单的方法吗?

class GUI(QObject):

sig_x = Signal()
sig_y = Signal(int)

def __init__(self,parent = None):
    super(GUI,self).__init__(parent)
    self.value = 10

    self.button = QPushButton("Click me")   
    self.button.clicked.connect(self.sig_x) 
    #self.button.clicked.connect(self.sig_y(self.value))  want to do something like this
    
    self.sig_x.connect(self.pass_args)
    self.sig_y.connect(self.final_function)

#connection slot for sig_x
def pass_args(self):                                                                                     
    self.sig_y.emit(self.value)  

#demo connection slot for sig_y 
def final_function(self,passed_value):
    print("passed value is " + str(passed_value))

The reason this won't work:这不起作用的原因:

self.button.clicked.connect(self.sig_y(self.value))

is because connect() needs to be passed a function (or, more precisely anything that can be called ).是因为connect()需要传递一个 function (或者,更准确地说,任何可以调用的东西)。 But written like this, you're not passing it the signal itself, you're calling the signal and passing in the result.但是这样写,你不是将信号本身传递给它,你是在调用信号并传递结果。

One solution is a lambda function .一种解决方案是lambda function A lambda is a way to define a function inline; lambda 是一种内联定义 function 的方法; this way you define a new function right there, in the same line where you pass it to connect .这样,您就在此处定义了一个新的 function ,在您将其传递给connect的同一行中。

self.button.clicked.connect(lambda x: self.sig_y(self.value))

You can see some more context of how this works in this quesiton , even though the question itself is about getting a slightly more complex version to work.你可以在这个问题中看到更多关于它是如何工作的上下文,即使这个问题本身是关于让一个稍微复杂的版本工作的。


This is all assuming that you need this signal structure for reasons not immediately evident in your example.这一切都假设您需要此信号结构,原因在您的示例中并未立即显现。 If not, you could simply connect the button clicked signal directly to your final_function , with no need for custom signals.如果没有,您可以简单地将按钮单击信号直接连接到您的final_function ,而无需自定义信号。

class GUI(QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.value = 10

        self.button = QPushButton("Click me")   
        self.button.clicked.connect(self.final_function)

    def final_function(self, state):
        print(f"My value is {self.value}.")

And even then, if you need a signal for notifying other things, you could also add that to the method:即使这样,如果您需要一个信号来通知其他事情,您也可以将其添加到方法中:

    def final_function(self, state):
        print(f"My value is {self.value}.")
        self.sig_y.emit(self.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM