Pyqt 防止组合框更改值 [英] Pyqt prevent combobox change value

查看:33
本文介绍了Pyqt 防止组合框更改值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PyQT4 中有四个组合框.如果用户更改第一个组合框中的值,则第二个组合框中的值也会更改,同样,如果第二个组合框中的值发生更改,则会导致第三个组合框的更改以及第四个组合框的情况相同.我想要的是当我更改第一个组合框的值时,它应该只导致第二个组合框的更改,而不会影响第三个和第四个组合框的更改.我怎样才能在 PyQt 中做到这一点?

I have four combo-box boxes that in PyQT4. If user change the value in first combo-box the values from second are altered and similarly if the value in second combo-box change, that results in the change of thirds combo-box and the same case for the fourth combo-box. What i want is when i change the value i first combo-box it should result in change of only second combo-box while does not effect the changes in third and fourth combo-box. How can i do this in PyQt ?

我在每个组合框上设置了 changedIndex 事件.

I have changedIndex event setup on each combo-box.

推荐答案

要防止对象在给定上下文中发出信号,您必须使用 blockSignals():

To prevent an object from issuing signals in a given context you must use blockSignals():

bool QObject.blockSignals (self, bool b)

如果 block 为真,则此对象发出的信号将被阻止(即,发出信号不会调用任何连接到它的东西).如果阻止为false,不会发生这种阻塞.

If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). If block is false, no such blocking will occur.

返回值是signalsBlocked()之前的值.

The return value is the previous value of signalsBlocked().

注意,destroy() 信号将被发射,即使信号此对象已被阻止.

Note that the destroyed() signal will be emitted even if the signals for this object have been blocked.

为了简化任务,setCurrentIndex() 方法将被覆盖.

To simplify the task, the setCurrentIndex() method will be overwritten.

class ComboBox(QComboBox):
    def setCurrentIndex(self, ix):
        self.blockSignals(True)
        QComboBox.setCurrentIndex(self, ix)
        self.blockSignals(False)

下面的例子展示了它的用法:

The following example shows its use:

class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QVBoxLayout())

        l = [str(i) for i in range(5)]
        cb1 = ComboBox(self)
        cb1.addItems(l)

        cb2 = ComboBox(self)
        cb2.addItems(l)

        cb3 = ComboBox(self)
        cb3.addItems(l)

        cb4 = ComboBox(self)
        cb4.addItems(l)

        cb1.currentIndexChanged.connect(cb2.setCurrentIndex)
        cb2.currentIndexChanged.connect(cb3.setCurrentIndex)
        cb3.currentIndexChanged.connect(cb4.setCurrentIndex)

        self.layout().addWidget(cb1)
        self.layout().addWidget(cb2)
        self.layout().addWidget(cb3)
        self.layout().addWidget(cb4)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

这篇关于Pyqt 防止组合框更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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