首页 > 解决方案 > 如何在 QML 中的 50 个文本字段之间切换焦点?

问题描述

当用户按 Enter 键时,我想将焦点从一个字段切换到下一个字段:我该怎么做?

项目按网格排列,如下所示:

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
      }
    }
  }
}

标签: javascriptqtqml

解决方案


由于TextField继承自Item您可以使用现有的焦点链使用nextItemInFocusChain(). 只需将以下行添加到TextField

Keys.onEnterPressed: nextItemInFocusChain().forceActiveFocus()

推荐阅读