Qt Forum

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

    How to set individual combo boxes currentindex?

    QML and Qt Quick
    2
    4
    866
    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.
    • G
      gnris last edited by

      Hello,I have some question about set the combobox currentindex.
      I write some code like this
      @
      ListModel {id: author1} //data may append from other model

      TableView {
      model: author1
      TableViewColumn{
      role: “attribute”
      title: “attribute”
      width: 180
      delegate:Rectangle {
      ComboBox {
      id:combo
      model:author1
      textRole: “attribute”
      anchors.fill: parent
      Component.onCompleted: {
      var i
      for(i=0;i<combo.count;i++)
      combo.currentIndex=i
      }
      }
      }
      }
      @

      Can I control each combobox in Component.onCompleted?
      The code I wrote above can only set the last element to be currentindex.
      I want to set each combobox currentindex according to the order in Listmodel.
      i.e. the second combobox should set its currentindex to 1 (currentindex start from 0)

      Thanks!

      1 Reply Last reply Reply Quote 0
      • G
        gnris last edited by

        OK..I got the answer.
        use
        @
        onCountChanged: {

        combo.currentIndex = combo.find(styleData.value);
        }
        @

        1 Reply Last reply Reply Quote 0
        • J
          Jens last edited by

          The delegate is created per row so you do not want a loop in there.

          What you are looking for is probably styleData.row which will give you the current row index of the delegate. Another issue is that you should not really rely on "onCompleted" for items in tableview delegates since the created instance can get recycled multiple times.

          @
          ComboBox {
          id:combo
          model:author1
          textRole: "attribute"
          anchors.fill: parent
          currentIndex: styleData.row
          }
          @

          1 Reply Last reply Reply Quote 0
          • G
            gnris last edited by

            Thanks for your suggestion and reply!!

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