[SOLVED]How to set ComboBox's current text in QML?
-
How to set a ComboBox's current text in QML?
I know it is easy to get the current text by myCombobox.currentText, but how to set it? I want to set the value in different cases.
Hi @CoderJeff,
If you are usingListModel
as amodel
forComboBox
then you can just assign the new text to thecurrentIndex
. For eg:comboModel.get(combo.currentIndex).text = "NewText" // comboModel = id of ListModel // combo = id of ComboBox
-
Thank you, p3c0.
I solved the problem according to your method.
Component.onCompleted: { currentIndex = find("The first list information"); comboModel.get(currentIndex).text = "The first list information" }
Actually, there is no new text, what I want to set is just one of the drop down list.
It works but it seems a little weird. Set a string through the string's index. It's like make an unnecessary move here. I do not know why QML does not provide more direct method.
-
Thank you, p3c0.
I solved the problem according to your method.
Component.onCompleted: { currentIndex = find("The first list information"); comboModel.get(currentIndex).text = "The first list information" }
Actually, there is no new text, what I want to set is just one of the drop down list.
It works but it seems a little weird. Set a string through the string's index. It's like make an unnecessary move here. I do not know why QML does not provide more direct method.
@CoderJeff You are Welcome :)
Actually usingget
fetches that object and thus allowing to access/modify its properties.
Well may be in future they would add direct way to do it.