Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to set individual combo boxes currentindex?
QtWS25 Last Chance

How to set individual combo boxes currentindex?

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 1.1k 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.
  • G Offline
    G Offline
    gnris
    wrote on last edited by
    #1

    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
    0
    • G Offline
      G Offline
      gnris
      wrote on last edited by
      #2

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

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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jens
        wrote on last edited by
        #3

        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
        0
        • G Offline
          G Offline
          gnris
          wrote on last edited by
          #4

          Thanks for your suggestion and reply!!

          1 Reply Last reply
          0

          • Login

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