1

I must be overlooking something really simple. I want to use a QComboxBox dropdown list to control the form input interpretation, rather than provide a choice of input values. Maybe this example would help clarify:

dropdown

The dropdown list would specify what weight units will be entered by the user. In other words, if the 'kilograms' list value were chosen, a subsequent entry of '10' by the user in the textedit area would be interpreted as 10 kilograms. As far as I can tell, there is no way to bind a QComboBox in this way. Suggestions?

1

I would suggest something like this. You can retrieve the value selected in the combo box and use it, along with a line edit value, in a string such as a label to give context to the value entered into the line edit (for display or user interpretation purposes), or convert the line edit value to an integer and manipulate it depending on the combo box value (e.g. for calculation purposes).

Try out this example code in the python console in Qgis 3:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class Dlg(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setGeometry(200, 200, 450, 250)
        self.lbl1 = QLabel('Enter a weight:', self)
        self.lbl1.move(5, 30)
        self.le1 = QLineEdit(self)
        self.le1.move(100, 30)
        self.lbl2 = QLabel('Units:', self)
        self.lbl2.move(250, 30)
        self.cb1 = QComboBox(self)
        self.cb1.addItems(['kilograms', 'pounds'])
        self.cb1.move(300, 30)
        self.lbl3 = QLabel('Value entered:', self)
        self.lbl3.setFont(QFont('Arial', 18))
        self.lbl3.adjustSize()
        self.lbl3.move(75, 150)
        self.btn_ok = QPushButton('Enter', self)
        self.btn_ok.move(175, 85)
        self.btn_ok.clicked.connect(self.enter)

    def enter(self):
        '''Retrieve values of line edit and combo box and either print them to a label etc for
        display purposes (commented line below) or use them in a conditional statement'''
#        self.lbl3.setText('Value entered: ' + self.le1.text() + ' ' + self.cb1.currentText())
        if self.cb1.currentText() == 'kilograms':
            self.lbl3.setText('Value entered: ' + self.le1.text()+'kg')
        elif self.cb1.currentText() == 'pounds':
            self.lbl3.setText('Value entered: ' + self.le1.text()+'lbs')
        self.lbl3.adjustSize()

w = Dlg()
w.show()

Images of resulting dialog below:

enter image description here

enter image description here

enter image description here

Additionally, let's say you are using the value entered in the line edit in a calculation. You could use the combo box value to manipulate the line edit value in the conditional statement. For example, if you are working in kilograms and want to convert values which were entered in pounds, you could do something like:

if self.cb1.currentText() == 'pounds':
    val = int(self.le1.text()) * 0.45
elif self.cb1.currentText() == 'kilograms':
    val = int(self.le1.text())
|improve this answer|||||
  • Ben, thanks for the feedback. I see where I wasn't clear in my request. What I want is when the user selects a combobox value like 'kilograms', then the value that is typed into the text box area is interpreted as kilograms. I don't want the word 'kilograms' to appear. What I want to avoid is having to provide separate textedit areas for either kg or pounds inputs-- seems to me that there should be some way of varying the interpretation of a textedit field. – Rudy Stricklan Jan 27 '19 at 7:10
  • 1
    @Rudy Stricklan, I have completely re-written my answer above. I'm still not exactly sure what you mean by varying the interpretation, since the value entered into a line edit is just data with an associated type (string, integer etc.) but you can, convert between data types and manipulate the value depending on the combo box value. – Ben W Jan 28 '19 at 2:00
  • Most excellent @Ben W! I follow your logic and see how it would accomplish what I was looking for. I marked your answer as the solution. Kudos for digging into it! – Rudy Stricklan Jan 28 '19 at 3:19
  • You're very welcome @Rudy Stricklan. I'm glad to hear that it sounds like my approach will fit the bill. – Ben W Jan 28 '19 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.