Error when several Actions with the same Shortcut

时间:2015-07-31 19:48:36

标签: qt qml qtquick2

I have a TabView. Each Tab is in the separate file (here for simplicity I composed all the code in one file). I'd like to launch some function using Enter key or a Button. All is ok when I click the Buttons. But when I press Enter, nothing happens (onTriggered event handler is never executed) and I also get an error:

QQuickAction::event: Ambiguous shortcut overload: Return

If I have only one Tab the problem does not occur and the onTriggered handler is correctly executed.

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

How can I solve it?

As a workaround I could use the following shortcut binding in the Action :

shortcut: tab1.activeFocus ? StandardKey.InsertParagraphSeparator : ""

But the problem is that, first I need (don't know why) to click on all tabs' headers, before events could trigger...

1 个答案:

答案 0 :(得分:2)

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