Qt Quick Controls 2中的互斥菜单项

时间:2019-04-25 00:28:06

标签: qt qt-quick qtquickcontrols2 qtquickcontrols

我想使用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

1 个答案:

答案 0 :(得分: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 }
}