How can I change the contents of one QComboBox depending on another QComboBox in PyQt5?

房东的猫 提交于 2019-12-04 21:17:53

You must use the signal currentTextChanged, this is activated every time you choose an item, returning the text, we must also validate if it is a list or a single element. All of the above is implemented in the following code:

    [...]
    self.comboBoxInputFirstParam = QtWidgets.QComboBox(self.groupBoxFirstParam)
    self.comboBoxInputFirstParamUnit = QtWidgets.QComboBox(self.groupBoxFirstParam)
    self.comboBoxInputFirstParam.currentTextChanged.connect(self.onCurrentTextChanged)

    self.comboBoxInputFirstParam.addItems(CalcProp.ChooseParams().paramList())

def onCurrentTextChanged(self, text):
    self.comboBoxInputFirstParamUnit.clear()
    elements = CalcProp.ChooseUnitOfMeasurement().unitOfMeasurement(str(text))
    if isinstance(elements, list):
        self.comboBoxInputFirstParamUnit.addItems(elements)
    else:
        self.comboBoxInputFirstParamUnit.addItem(elements)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!