How can I change the contents of one QComboBox dep

2019-06-09 21:36发布

问题:

I write program on python3 that will calculate thermodynamic properties. This is piece of GUI

"Выбранное вещество" is a selected substance like 1-Butene, water, ammonia and etc.

"Первый параметр" is a first parameter. User will choose parameters like pressure, temperature, density and etc, and unit of measurement like Pa, MPa, bar (if it's pressure) and etc. So I don't know one thing: i want that if user chose pressure('Давление (P)') in top combobox, depending on this, suitable units of measure were chosen in small combobox.

What have i already done: create 2 files

  • testGUI.py - there is all GUI (i create in Qt Designer)
  • CalcProp.py - there is all logic of program (classes and functions)

So in CalcProp.py i wrote class that contains function that returns the list of parameters:

class ChooseParams():
   def paramList(self):
      P = 'Pressure (P)'
      T = 'Temperature (T)'
      D = 'Density (D)'
      V = 'Volume (V)'
      H = 'Enthalpy (h)'
      S = 'Entropy (s)'
      Q = 'Vapor quality (x)'

      allParams = [P, T, D, V, H, S, Q]

      return allParams

After i created class that contains function that selects unit of measurements:

class ChooseUnitOfMeasurement():
    def unitOfMeasurement(self, parameter):

        #Pressure
        Pa = 'Па'
        kPa = 'кПа'
        MPa = 'МПа'
        PressureUnitList = [Pa, kPa, MPa]

        #Temperature
        kelvin = 'К'
        degC = '°C'
        degF = '°F'
        tempUnitList = [kelvin, degC, degF]

        #Enthalpy
        kJdivKg = 'кДж/кг'
        JdivKg = 'Дж/кг'
        enthalpyUnitList = [kJdivKg, JdivKg]

        #Entropy
        kJdivKgKel = 'кДж/(кг-К)'
        JdivKgKel = 'Дж/(кг-К)'
        entropyUnitList = [kJdivKgKel, JdivKgKel]

        #Density
        kgDivMeter = 'кг/м^3'

        #Volume
        meterDivKg = 'м^3/кг'

        #Vapor quality
        vaporQuality = '--'


        if parameter == 'Pressure (P)':
            return PressureUnitList
        elif parameter == 'Temperature (T)':
            return tempUnitList
        elif parameter == 'Density (D)':
            return kgDivMeter
        elif parameter == 'Volume (V)':
            return meterDivKg
        elif parameter == 'Enthalpy (h)':
            return enthalpyUnitList
        elif parameter == 'Entropy (s)':
            return entropyUnitList
        else:
            return vaporQuality

in testGUI.py

#Creation combobox for selection first parameter
self.comboBoxInputFirstParam = QtWidgets.QComboBox(self.groupBoxFirstParam)
#put parameters
self.comboBoxInputFirstParam.addItems(CalcProp.ChooseParams.paramList(self))

#Creation combobox for selection unit of measurement (first parameter)
self.comboBoxInputFirstParamUnit = QtWidgets.QComboBox(self.groupBoxFirstParam)
#get text of first combobox
firstParameter = self.comboBoxInputFirstParam.currentText()
#Depending on the content of the first one, add the required list / value in the combobox with units of measurement.
self.comboBoxInputFirstParamUnit.addItems(CalcProp.ChooseUnitOfMeasurement.unitOfMeasurement(self, firstParameter))

Everything works, but that's only when the program starts, when I change the pressure to another value, then unit of measurement does not change. And I'm interested in how to change the contents of one combobox depending on another in real time.

回答1:

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)