Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED]How to set ComboBox's current text in QML?

    General and Desktop
    2
    4
    5786
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      CoderJeff last edited by CoderJeff

      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.

      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @CoderJeff last edited by

        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
        

        157

        1 Reply Last reply Reply Quote 0
        • C
          CoderJeff last edited by p3c0

          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.

          p3c0 1 Reply Last reply Reply Quote 1
          • p3c0
            p3c0 Moderators @CoderJeff last edited by

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

            157

            1 Reply Last reply Reply Quote 0
            • First post
              Last post