Ankush
Ankush

Reputation: 53

Using signal from comboBox to disable QspinBox in Pyqt

I have comboBox as one of widget and QspinBox as another widget. I want to disable QspinBox widget if we change option in available in comboBox widget. As an example in my code given below if I change option from option_1 to option_2, QspinBox widget need to get disabled. So how can I do it..?? Any help with example will be appreciated. My code is as follows,

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_tri_combobox(object):
    def setupUi(self, tri_combobox):
        tri_combobox.setObjectName(_fromUtf8("tri_combobox"))
        tri_combobox.resize(686, 510)
        self.centralWidget = QtGui.QWidget(tri_combobox)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
        self.comboBox = QtGui.QComboBox(self.centralWidget)
        self.comboBox.setGeometry(QtCore.QRect(50, 130, 221, 27))
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.spinBox = QtGui.QSpinBox(self.centralWidget)
        self.spinBox.setGeometry(QtCore.QRect(360, 130, 251, 27))
        self.spinBox.setObjectName(_fromUtf8("spinBox"))
        tri_combobox.setCentralWidget(self.centralWidget)
        self.menuBar = QtGui.QMenuBar(tri_combobox)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 686, 25))
        self.menuBar.setObjectName(_fromUtf8("menuBar"))
        tri_combobox.setMenuBar(self.menuBar)
        self.mainToolBar = QtGui.QToolBar(tri_combobox)
        self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
        tri_combobox.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtGui.QStatusBar(tri_combobox)
        self.statusBar.setObjectName(_fromUtf8("statusBar"))
        tri_combobox.setStatusBar(self.statusBar)

        self.retranslateUi(tri_combobox)
        QtCore.QMetaObject.connectSlotsByName(tri_combobox)

    def retranslateUi(self, tri_combobox):
        tri_combobox.setWindowTitle(_translate("tri_combobox", "tri_combobox", None))
        self.comboBox.setItemText(0, _translate("tri_combobox", "option_1", None))
        self.comboBox.setItemText(1, _translate("tri_combobox", "option_2", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    tri_combobox = QtGui.QMainWindow()
    ui = Ui_tri_combobox()
    ui.setupUi(tri_combobox)
    tri_combobox.show()
    sys.exit(app.exec_())

Upvotes: 2

Views: 1476

Answers (1)

jkerian
jkerian

Reputation: 17056

You're looking for the wonderful world of Qt signals and slots.

In simple code terms, this is the code you want executed when someone selects an element from the comboBox.

def dropdownSelect(self, index):
    self.spinBox.setEnabled(not index)

Of course a non-toy example will use a more complicated series of if-statements, but the general idea is the same. In this case, index 0 is the first element, 1 is option_1, etc. Add this function to your Ui class.

Now link it by adding this line to setupUi:

    self.comboBox.currentIndexChanged.connect(self.dropdownSelect)

Here's the documentation on this particular signal. What's going on here is that you're telling Qt that when the comboBox value is adjusted, you have a special handling function for that. The Qt core event loop handles managing all of that. The signal is a "pattern", that tells you which parameters you'll have access to in your slot.

Upvotes: 3

Related Questions