Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[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.


  • Moderators

    Hi @CoderJeff,
    If you are using ListModel as a model for ComboBox then you can just assign the new text to the currentIndex. 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.


  • Moderators

    @CoderJeff You are Welcome :)
    Actually using get fetches that object and thus allowing to access/modify its properties.
    Well may be in future they would add direct way to do it.


Log in to reply