使用来自 comboBox 的信号禁用 Pyqt 中的 QspinBox

Using signal from comboBox to disable QspinBox in Pyqt

我将 comboBox 作为一个小部件,将 QspinBox 作为另一个小部件。如果我们更改 comboBox 小部件中可用的选项,我想禁用 QspinBox 小部件。作为下面给出的代码示例,如果我将选项从 option_1 更改为 option_2,QspinBox 小部件需要被禁用。那我该怎么做..?任何有关示例的帮助将不胜感激。我的代码如下,

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_())

你在寻找Qt signals and slots的精彩世界。

用简单的代码术语来说,这是当有人从组合框中选择一个元素时您要执行的代码。

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

当然,非玩具示例将使用更复杂的一系列 if 语句,但总体思路是相同的。在这种情况下,索引 0 是第一个元素,1 是 option_1,等等。将此函数添加到您的 Ui class.

现在 link 通过将此行添加到设置 Ui:

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

Here's 有关此特定信号的文档。这里发生的事情是您告诉 Qt,当组合框值被调整时,您有一个特殊的处理函数。 Qt 核心事件循环负责管理所有这些。信号是 "pattern",它告诉您可以在插槽中访问哪些参数。