多个操作具有相同快捷方式时出错
Error when several Actions with the same Shortcut
我有一个TabView
。每个 Tab
都在单独的文件中(这里为简单起见,我将所有代码组合在一个文件中)。我想使用 Enter 键或 Button
启动一些功能。单击 Button
s 时一切正常。但是当我按下 Enter 时,没有任何反应(onTriggered
事件处理程序从不执行)并且我也收到错误消息:
QQuickAction::event: Ambiguous shortcut overload: Return
如果我只有一个 Tab
问题就不会发生并且 onTriggered
处理程序被正确执行。
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
Item {
width: 640
height: 480
TabView {
id: tabView
currentIndex: 0
anchors.fill: parent
Layout.minimumWidth: 360
Layout.minimumHeight: 360
Layout.preferredWidth: 480
Layout.preferredHeight: 640
Tab {
id: tab1
active: true
title: "One"
Item {
id: item
x: 16
y: 8
width: 640
height: 480
Action {
id: calcDataAction
text: "Run"
shortcut: StandardKey.InsertParagraphSeparator
tooltip: "one"
onTriggered: {
console.log("one")
}
}
Button {
action: calcDataAction
id: calcButton
x: 20
y: 20
height: 40
width: 100
}
}
}
Tab {
id: tab2
active: true
title: "Two"
Item {
id: item2
x: 16
y: 8
width: 640
height: 480
Action {
id: calcDataAction2
text: "Run"
shortcut: StandardKey.InsertParagraphSeparator
tooltip: "two"
onTriggered: {
console.log("two")
}
}
Button {
action: calcDataAction2
id: calcButton2
x: 20
y: 20
height: 40
width: 100
}
}
}
}
}
我该如何解决?
作为解决方法,我可以在 Action
中使用以下 shortcut
绑定:
shortcut: tab1.activeFocus ? StandardKey.InsertParagraphSeparator : ""
但问题是,首先我需要(不知道为什么)单击所有选项卡 headers,然后事件才能触发...
Action
有一个 属性 enabled
,就像 QML 中几乎所有的视觉和非视觉类型一样。如果启用 - 默认情况下 - 可以触发 Action
。
同时激活所有 Action
没有意义,因为只有一个 Tab
可见。因此,解决该问题的一种方法是一次仅启用 一个 Action
,即与当前可见的 tab
关联的那个:
enabled: <tab_id>.visible
按照您的代码,一个最小的示例如下所示:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
ApplicationWindow {
width: 300
height: 200
visible: true
TabView {
id: tabView
anchors.fill: parent
Tab {
id: tab1
title: "One"
Action {
id: calcDataAction
enabled: tab1.visible
shortcut: "Ctrl+O"
onTriggered: console.log(tab1.title)
}
}
Tab {
id: tab2
title: "Two"
Action {
id: calcDataAction2
enabled: tab2.visible
shortcut: "Ctrl+O"
onTriggered: console.log(tab2.title)
}
}
}
}
我有一个TabView
。每个 Tab
都在单独的文件中(这里为简单起见,我将所有代码组合在一个文件中)。我想使用 Enter 键或 Button
启动一些功能。单击 Button
s 时一切正常。但是当我按下 Enter 时,没有任何反应(onTriggered
事件处理程序从不执行)并且我也收到错误消息:
QQuickAction::event: Ambiguous shortcut overload: Return
如果我只有一个 Tab
问题就不会发生并且 onTriggered
处理程序被正确执行。
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
Item {
width: 640
height: 480
TabView {
id: tabView
currentIndex: 0
anchors.fill: parent
Layout.minimumWidth: 360
Layout.minimumHeight: 360
Layout.preferredWidth: 480
Layout.preferredHeight: 640
Tab {
id: tab1
active: true
title: "One"
Item {
id: item
x: 16
y: 8
width: 640
height: 480
Action {
id: calcDataAction
text: "Run"
shortcut: StandardKey.InsertParagraphSeparator
tooltip: "one"
onTriggered: {
console.log("one")
}
}
Button {
action: calcDataAction
id: calcButton
x: 20
y: 20
height: 40
width: 100
}
}
}
Tab {
id: tab2
active: true
title: "Two"
Item {
id: item2
x: 16
y: 8
width: 640
height: 480
Action {
id: calcDataAction2
text: "Run"
shortcut: StandardKey.InsertParagraphSeparator
tooltip: "two"
onTriggered: {
console.log("two")
}
}
Button {
action: calcDataAction2
id: calcButton2
x: 20
y: 20
height: 40
width: 100
}
}
}
}
}
我该如何解决?
作为解决方法,我可以在 Action
中使用以下 shortcut
绑定:
shortcut: tab1.activeFocus ? StandardKey.InsertParagraphSeparator : ""
但问题是,首先我需要(不知道为什么)单击所有选项卡 headers,然后事件才能触发...
Action
有一个 属性 enabled
,就像 QML 中几乎所有的视觉和非视觉类型一样。如果启用 - 默认情况下 - 可以触发 Action
。
同时激活所有 Action
没有意义,因为只有一个 Tab
可见。因此,解决该问题的一种方法是一次仅启用 一个 Action
,即与当前可见的 tab
关联的那个:
enabled: <tab_id>.visible
按照您的代码,一个最小的示例如下所示:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
ApplicationWindow {
width: 300
height: 200
visible: true
TabView {
id: tabView
anchors.fill: parent
Tab {
id: tab1
title: "One"
Action {
id: calcDataAction
enabled: tab1.visible
shortcut: "Ctrl+O"
onTriggered: console.log(tab1.title)
}
}
Tab {
id: tab2
title: "Two"
Action {
id: calcDataAction2
enabled: tab2.visible
shortcut: "Ctrl+O"
onTriggered: console.log(tab2.title)
}
}
}
}