Reputation: 343
Using PyQt4, I want to hide/disable a combobox when we select option 3 from another combobox.
I know that we can use QComboBox.setEnabled(False)
to hide/disable the combobox but I don't know how to implement it in my code.
I tried to do this in QT Designer using signal slot editor but there is no option to send a signal from combobox when we select any particular element.
This the GUI Code:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_test_gui(object):
def setupUi(self, test_gui):
test_gui.setObjectName(_fromUtf8("test_gui"))
test_gui.resize(400, 300)
self.primarybox = QtGui.QComboBox(test_gui)
self.primarybox.setGeometry(QtCore.QRect(30, 70, 69, 22))
self.primarybox.setObjectName(_fromUtf8("primarybox"))
self.primarybox.addItem(_fromUtf8(""))
self.primarybox.addItem(_fromUtf8(""))
self.primarybox.addItem(_fromUtf8(""))
self.primarybox.addItem(_fromUtf8(""))
self.primarybox.addItem(_fromUtf8(""))
self.secondarybox = QtGui.QComboBox(test_gui)
self.secondarybox.setGeometry(QtCore.QRect(210, 70, 69, 22))
self.secondarybox.setObjectName(_fromUtf8("secondarybox"))
self.secondarybox.addItem(_fromUtf8(""))
self.secondarybox.addItem(_fromUtf8(""))
self.secondarybox.addItem(_fromUtf8(""))
self.secondarybox.addItem(_fromUtf8(""))
self.retranslateUi(test_gui)
QtCore.QMetaObject.connectSlotsByName(test_gui)
def retranslateUi(self, test_gui):
test_gui.setWindowTitle(_translate("test_gui", "Dialog", None))
self.primarybox.setItemText(0, _translate("test_gui", "Comb1", None))
self.primarybox.setItemText(1, _translate("test_gui", "option1", None))
self.primarybox.setItemText(2, _translate("test_gui", "option2", None))
self.primarybox.setItemText(3, _translate("test_gui", "option3", None))
self.primarybox.setItemText(4, _translate("test_gui", "option4", None))
self.secondarybox.setItemText(0, _translate("test_gui", "comb2", None))
self.secondarybox.setItemText(1, _translate("test_gui", "element1", None))
self.secondarybox.setItemText(2, _translate("test_gui", "element2", None))
self.secondarybox.setItemText(3, _translate("test_gui", "element3", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
test_gui = QtGui.QDialog()
ui = Ui_test_gui()
ui.setupUi(test_gui)
test_gui.show()
sys.exit(app.exec_())
Upvotes: 1
Views: 2681
Reputation: 6075
QComboBox
has a signal currentIndexChanged
. It is emitted when the user select a new choice in a combobox. The new index is passed with the signal.
You can connect this signal to a function that will disable the second combobox if the new index is 2 (aka option 3)
self.comboBox1.currentIndexChanged.connect(self.on_change)
def on_change(self, newIndex):
if newIndex==2:
self.comboBox2.setEnabled(False)
Upvotes: 2