Qt Quick Controls 2中的互斥菜单项

问题描述 投票:0回答:1

我想在子菜单中指定互斥项目,使用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")
        }
    }
}

Should only be able to check a single item in View -> Shading

qt qt-quick qtquickcontrols2 qtquickcontrols
1个回答
0
投票

通过做这个:

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

© www.soinside.com 2019 - 2024. All rights reserved.