PyQt 将 SIGNAL 连接到多个 SLOT [英] PyQt connect SIGNAL to multiple SLOT

查看:139
本文介绍了PyQt 将 SIGNAL 连接到多个 SLOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从使用python和qt的快速gui编程学习PyQt,目前正在学习SignalsSlots>.

I am trying to learn PyQt from rapid gui programming with python and qt and currently learning Signals and Slots.

下面是我的代码片段:

self.connect(self.dial, SIGNAL("valueChanged(int)"),self.spinbox.setValue) #1
self.connect(self.dial, SIGNAL("valueChanged(int)"),self.getValue_dial)    #2
self.connect(self.spinbox, SIGNAL("valueChanged(int)"),self.dial.setValue)
self.connect(self.spinbox, SIGNAL("valueChanged(int)"),self.getValue_spinbox)

def getValue_dial(self):
        print self.dial.value()

def getValue_spinbox(self):
        print self.dial.value()

我在这里试图实现的是立即调用 2 SLOTS,即 spinbox.setValuegetValue_dial 用于 dial 对象一发出 ValueChanged(int) 信号.

What I am trying to achieve here is call 2 SLOTS at once that is spinbox.setValue and getValue_dial for dial object as soon as ValueChanged(int) signal is emitted.

上面的代码成功执行,没有任何错误,并在更改时打印适当的值.

The above code executes successfully without any errors and print the appropriate values as they are changed.

现在我的问题是上述方法适合为单个信号调用多个插槽.?

Now my question is the above way appropriate to call multiple slots for a single signal.?

以上两条语句(1 & 2)能否合并为一条语句.

Can the above two statements(1 & 2) be combined into a single statement.

这是我的完整代码的链接.

推荐答案

你这样做的方式很好.如果您有很多事情要做,您可以连接到一个新函数来为您处理所有事情.

The way you're doing it is fine. If you had quite a few things to do you could connect to a new function that handles everything for you.

我注意到在您连接的 getValue 函数中,您直接从对象获取值;您是否知道该值是作为带有 valueChanged(int) 信号的参数传递的?如果您更改 getValue 函数以接受附加参数,则无需直接从对象获取值.当然,您可以一起取消 getValue 函数,并在辅助函数中发出打印语句.

I notice in your connected getValue functions you're getting the value straight from the object; are you aware that the value is passed as a parameter with the valueChanged(int) signal? If you change your getValue functions to accept an additional parameter there will be no need to get the value directly from the object. Of course you could do away with the getValue function all together and issue your print statement in the helper function.

self.connect(self.dial, SIGNAL("valueChanged(int)"), self.dial_value_changed)
self.connect(self.spinbox, SIGNAL("valueChanged(int)"),self.spinbox_value_changed)

def dial_value_changed(self, value):
    self.spinbox.setValue(value)
    self.getValue_dial(value)

def spinbox_value_changed(self, value):
    self.dial.setValue(value)
    self.getValue_spinbox(value)

def getValue_dial(self, value):
        print value

def getValue_spinbox(self, value):
        print value

此外,这取决于偏好,有一种信号和slot 可以让代码更容易阅读.它会改变

Also, and this is down to preference, there is a new style for signals and slots which can make the code a little easier to read. It would change the

self.connect(self.dial, SIGNAL("valueChanged(int)"), self.dial_value_changed)
self.connect(self.spinbox, SIGNAL("valueChanged(int)"),self.spinbox_value_changed)

上面的行

self.dial.valueChanged.connect(self.dial_value_changed)
self.spinbox.valueChanged.connect(self.spinbox_value_changed)

但是对于最初的问题和你正在做的两件事,我只会连接信号两次,而不是使用辅助函数.

But for the original question and for the two things you're doing I'd just connect the signal twice rather than have a helper function.

这篇关于PyQt 将 SIGNAL 连接到多个 SLOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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