未触发 QtQuick2 快捷方式

QtQuick2 Shortcut not triggered

我正在使用 Qt 5.8.0 向 QtQuick Controls2 应用程序添加键盘快捷键,我想像这样使用 QKeySequence 来控制标签栏:

ApplicationWindow {
  ...
  Shortcut {
      sequence: StandardKey.NextChild
      onActivated: tabBar.nextTab()
  }

  Shortcut {
      sequence: StandardKey.PreviousChild
      onActivated: tabBar.previousTab()
  }
}

TabBar {
  id: tabBar
  ...
  function nextTab() {
    console.log("next tab")
    if((currentIndex + 1) < contentChildren.length)
      currentIndex += 1
    else
      currentIndex = 0
  }

  function previousTab() {
    console.log("previous tab")
    if((currentIndex - 1) > 0)
      currentIndex -= 1
    else
      currentIndex = contentChildren.length - 1
  }
}

这适用于使用 Ctrl+Tab 的 NextChild 序列,但是 PreviousChild 序列不起作用。我检查了 documentation,它在 Windows 中声称 previousChild 序列是 Ctrl+Shift+Tab,正如我所期望的那样。

我添加了一个 console.log() 来检查函数是否被调用,而实际上它没有被调用。 由于我对这两个函数使用相同的代码,所以我只能假设键序列是错误的,或者我还遗漏了什么?

这似乎是一个 Qt 错误 https://bugreports.qt.io/browse/QTBUG-15746

或者,您可以将 previousChild 快捷方式定义为

Shortcut {
    sequence:  "Ctrl+Shift+Tab"
    onActivated: {
        tabBar.previousTab()
    }
}

这不是重点,但是您的 previousTab 实现中存在一个小索引错误

function previousTab() {
    console.log("previous tab")
    if(currentIndex > 0) // Instead of (currentIndex - 1) > 0
        currentIndex--
    else
        currentIndex = contentChildren.length-1
}