Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]How to set ComboBox's current text in QML?

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 7.0k Views
  • 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 Offline
    C Offline
    CoderJeff
    wrote on 30 Apr 2015, 06:00 last edited by CoderJeff 5 Mar 2015, 11:08
    #1

    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.

    P 1 Reply Last reply 30 Apr 2015, 06:21
    0
    • C CoderJeff
      30 Apr 2015, 06:00

      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.

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 30 Apr 2015, 06:21 last edited by
      #2

      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
      0
      • C Offline
        C Offline
        CoderJeff
        wrote on 30 Apr 2015, 07:20 last edited by p3c0
        #3

        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.

        P 1 Reply Last reply 30 Apr 2015, 07:31
        1
        • C CoderJeff
          30 Apr 2015, 07:20

          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.

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 30 Apr 2015, 07:31 last edited by
          #4

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

          3/4

          30 Apr 2015, 07:20

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved