How to switch focus among 50 text fields in QML?

I want to switch focus from one field to the next when the user presses Enter: how would I go about it?

The items are arranged in a grid, as follows:

Grid {
  x: 5
  y: 3
  rows: 5
  columns: 20
  spacing: 10

  Repeater { 
    model: 50

    TextField { 
      width: 28
      height: 50
      color: "green"
      text: "0000"
      font.pixelSize: 12

      validator: IntValidator {
        bottom: -256
        top: 256
      }
    }
  }
}

Answers:

Answer

Since TextField inherits from Item you can use the existing focus chain using nextItemInFocusChain(). Just add the following line to the TextField:

Keys.onEnterPressed: nextItemInFocusChain().forceActiveFocus()
Answer

You can do so by using the Key attached property:

Grid {
    x: 5
    y: 3
    rows: 5
    columns: 20
    spacing: 10

    Repeater {
        id: rpt
        model: 50

        TextField {
            width: 28
            height: 50
            color: "green"
            text: "0000"
            font.pixelSize: 12

            Keys.onEnterPressed: rpt.itemAt(index + 1).focus = true

            validator: IntValidator {
                bottom: -256
                top: 256
            }
        }
    }
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.