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 ?
I have changedIndex event setup on each combo-box.
To prevent an object from issuing signals in a given context you must use blockSignals()
:
bool QObject.blockSignals (self, bool b)
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.
The return value is the previous value of signalsBlocked().
Note that the destroyed() signal will be emitted even if the signals for this object have been blocked.
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_())