简体   繁体   English

传递 PyQt QLineEdit 文本作为参数

[英]Passing PyQt QLineEdit text as parameter

I'm trying to edit a QLineEdit widget and then send the new text as a parameter to a function, but it's always returning the original text instead of the new text.我正在尝试编辑 QLineEdit 小部件,然后将新文本作为参数发送到 function,但它始终返回原始文本而不是新文本。 Here's a snippet of the code这是代码片段

self.shortCoffee1Label = QLineEdit("0")
self.shortCoffee1Label.editingFinished.connect(self.sendValue(48, int(self.shortCoffee1Label.text())))

and then the sendValue function然后是 sendValue function

    def sendValue(self, var, val):
        def emmitValue():
            self.messageReceived.setText("Sending new value...")
            print("var received: " + str(var))
            print("val received: " + str(val))
            self.thread.sendValue(var, val)
        return emmitValue

But val is always 0, which is the original value it was set to.但 val 始终为 0,这是它设置的原始值。 What am I doing wrong?我究竟做错了什么?

If you do like that the program will store the value "0" and attach it to editingFinished instead of refreshing it every time you call the function.如果您愿意,程序将存储值“0”并将其附加到 editingFinished 而不是每次调用 function 时都刷新它。 So you should use lambda (sorry for my bad English):所以你应该使用 lambda (对不起我的英语不好):

self.shortCoffee1Label = QLineEdit("0")
self.shortCoffee1Label.editingFinished.connect(lambda: self.sendValue(48, int(self.shortCoffee1Label.text())))

Got it.知道了。

Here's the final code:这是最终的代码:

self.shortCoffee1Label.editingFinished.connect(self.sendValue(48, self.shortCoffee1Label))

 def sendValue(self, var, val):
        def emmitValue():
            self.messageReceived.setText("Sending new value...")
            print("var received: " + str(var))
            print("val received: " + str(int(val.text())))
            self.thread.sendValue(var, int(val.text()))
        return emmitValue

So the idea was to send the object itself as the parameter so I could retrieve the updated text.所以想法是将 object 本身作为参数发送,这样我就可以检索更新的文本。 As it turns out, it might be interpreting that as a "send the text that this object had at compile".事实证明,它可能将其解释为“发送此 object 在编译时具有的文本”。 By sending the object itself, the problem was resolved.通过发送 object 本身,问题得到解决。

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

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