【问题标题】:QtQuick2 Shortcut not triggeredQtQuick2快捷方式未触发
【发布时间】:2018-01-18 10:17:19
【问题描述】:

我正在使用 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 qml qt5 qtquick2


    【解决方案1】:

    这似乎是一个 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2021-07-11
      • 2015-01-09
      • 2016-10-25
      相关资源
      最近更新 更多