Qt Quick Controls 2 中的互斥菜单项

Mutually exclusive menu items in Qt Quick Controls 2

我想使用 Qt Quick Controls 2 在子菜单中指定相互排斥的项目。 如何做到这一点?我怀疑它以某种方式涉及 ActionGroup,但我看不出子菜单如何引用 ActionGroup。在接下来的 main.qml 中,我定义了一个 ActionGroup "shadeActions" 和一个名为 "Shading" 的子菜单。如何将 shadeActions ActionGroup 合并到 Shading 菜单中?或者有其他方法可以使用 Qt Controls 2 来实现吗?

main.qml:

import QtQuick 2.9
import QtQuick.Controls 2.5

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("mbgrdviz")


    ActionGroup {
        id: shadeActions
        exclusive: true
        Action {checkable: true; text: qsTr("Off") }
        Action {checkable: true; text: qsTr("Slope") }
        Action {checkable: true; text: qsTr("Illumination") }
    }


    menuBar: MenuBar {
       Menu {
           title: qsTr("&File")
           Action { text: qsTr("&Open overlay grid...") }
           Action { text: qsTr("&Open site...") }
           Action { text: qsTr("&Open route...") }
           Action { text: qsTr("&Open navigation...") }
           MenuSeparator { }
           Action { text: qsTr("&Exit") }
       }
       Menu {
           title: qsTr("&View")
           Action { checkable: true; text: qsTr("&Map") }
           Action { checkable: true; text: qsTr("&Topography") }
           Action { checkable: true; text: qsTr("&Slope") }
           Action { checkable: true; text: qsTr("&Histograms") }
           Action { checkable: true; text: qsTr("&Contours") }
           Action { checkable: true; text: qsTr("&Sites") }
           Action { checkable: true; text: qsTr("&Routes") }
           Action { checkable: true; text: qsTr("&Vector") }
           Action { checkable: true; text: qsTr("&Profile window") }
           MenuSeparator {}
           Menu {
               title: "Shading"    // menu items should be exclusively selectable
               Action {checkable: true; text: qsTr("Off") }
               Action {checkable: true; text: qsTr("Slope")}
               Action {checkable: true; text: qsTr("Illumination") }
           }
       }
       Menu {
           title: qsTr("&Help")
           Action { text: qsTr("&About") }
       }
   }


    SwipeView {
        id: swipeView
        anchors.fill: parent
        currentIndex: tabBar.currentIndex

        Page1Form {
        }

        Page2Form {
        }
    }

    footer: TabBar {
        id: tabBar
        currentIndex: swipeView.currentIndex

        TabButton {
            text: qsTr("Page 1")
        }
        TabButton {
            text: qsTr("Page 2")
        }
    }
}

通过这样做:

    ActionGroup {
        id: shadeActions
        exclusive: true
        Action {checkable: true; text: qsTr("Off") }
        Action {checkable: true; text: qsTr("Slope") }
        Action {checkable: true; text: qsTr("Illumination") }
    }

您正在重复操作,菜单中的操作未分组。因此,无需操作即可创建您的群组:

ActionGroup {
        id: shadeActions
}

然后,将您的操作分配给组:

Menu {
   title: "Shading"    // menu items should be exclusively selectable
       Action {checkable: true; text: qsTr("Off"); ActionGroup.group: shadeActions  }
       Action {checkable: true; text: qsTr("Slope"); ActionGroup.group: shadeActions }
       Action {checkable: true; text: qsTr("Illumination"); ActionGroup.group: shadeActions }
}