How do you specify values for a ComboBox?
-
I have a combo box that looks like
ComboBox { id: gridCombo model: ListModel { id: gridModel ListElement { text: "1/1" } ListElement { text: "1/2" } ListElement { text: "1/4" } ListElement { text: "1/8" } } }
I would like to associate integers with each label equal to the denominator of each label. Is there a way to do this? If I try adding a 'value' field, the combo box displays blank entries.
I've also tried this alternate syntax, but it also displays blanks:
ComboBox { id: gridCombo model: [ { text: firstValue, value: firstValue }, { text: secondValue, value: secondValue }, { text: thirdValue, value: thirdValue } ] }
-
Yes I have. It didn't help. Like I said in my post, the second form isn't displaying anything.
@kitfox said in How do you specify values for a ComboBox?:
Yes I have. It didn't help. Like I said in my post, the second form isn't displaying anything.
Then you don't have read it carefully, you have to define
textRole
andvalueRole
, cf https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html#combobox-model-roles:ComboBox { textRole: "text" valueRole: "value" id: gridCombo model: [ { text: firstValue, value: firstValue }, { text: secondValue, value: secondValue }, { text: thirdValue, value: thirdValue } ] }
-
Have you read the documentation for ComboBox QML?; https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html
-
I have a combo box that looks like
ComboBox { id: gridCombo model: ListModel { id: gridModel ListElement { text: "1/1" } ListElement { text: "1/2" } ListElement { text: "1/4" } ListElement { text: "1/8" } } }
I would like to associate integers with each label equal to the denominator of each label. Is there a way to do this? If I try adding a 'value' field, the combo box displays blank entries.
I've also tried this alternate syntax, but it also displays blanks:
ComboBox { id: gridCombo model: [ { text: firstValue, value: firstValue }, { text: secondValue, value: secondValue }, { text: thirdValue, value: thirdValue } ] }
@kitfox said in How do you specify values for a ComboBox?:
I've also tried this alternate syntax, but it also displays blanks:
ComboBox { id: gridCombo model: [ { text: firstValue, value: firstValue }, { text: secondValue, value: secondValue }, { text: thirdValue, value: thirdValue } ] }
According to the documentation:
When using models that have multiple named roles, ComboBox must be configured to use a specific text role for its display text and delegate instances. If you want to use a role of the model item that corresponds to the text role, set valueRole. The currentValue property and indexOfValue() method can then be used to get information about those values.
Unless
textRole
andvalueRole
are set, the ComboBox doesn't know which role to use for each purpose. -
Yes I have. It didn't help. Like I said in my post, the second form isn't displaying anything.
@kitfox said in How do you specify values for a ComboBox?:
Yes I have. It didn't help. Like I said in my post, the second form isn't displaying anything.
Then you don't have read it carefully, you have to define
textRole
andvalueRole
, cf https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html#combobox-model-roles:ComboBox { textRole: "text" valueRole: "value" id: gridCombo model: [ { text: firstValue, value: firstValue }, { text: secondValue, value: secondValue }, { text: thirdValue, value: thirdValue } ] }