QML:如何从ApplicationWindow菜单栏中隐藏菜单项

huangapple go评论105阅读模式
英文:

QML: How to hide a Menu item from the ApplicationWindow MenuBar

问题

在QML/Qt6.5中,我尝试在ApplicationWindowMenuBar中动态添加/移除Menu,但我找不到解决方案。我可以隐藏MenuItems,但无法隐藏菜单本身。

ApplicationWindow
{
    id: mainWindow
    width: 600
    height: 400
    visible: true

    menuBar: MenuBar {
        id: mainMenuBar

        // this item should be shown/hidden at runtime
        Menu {
            id: subMenu
            title: "Menu to hide"

            MenuItem {
                text: "Menu Item"
            }
        }
    }
}

我尝试了几种方法。到目前为止,最好的方法是将标题设置为空字符串,但在这种情况下,菜单仍然存在(如果你将鼠标悬停在该区域,你仍然可以看到它)。有人有解决这个问题的方法吗?

将高度设置为0也不起作用。

英文:

In QML/Qt6.5 I try to add/remove a Menu from the MenuBar of a ApplicationWindow dynamically and I don't find a solution. I can hide MenuItems but not the Menu itself.

ApplicationWindow
{
    id: mainWindow
    width: 600
    height: 400
    visible: true

    menuBar: MenuBar {
        id: mainMenuBar

        // this item should be shown/hidden at runtime
        Menu {
            id: subMenu
            title: "Menu to hide"

            MenuItem {
                text: "Menu Item"
            }
        }
    }
}

I have tried several things. Best so far was setting the title to an empty string, but in this case the Menu is still there (you can see it if you hover the area). Anyone having a solution for that?

Setting the height to 0 is also not working properly.

答案1

得分: 1

你可以使用takeMenu()insertMenu()方法,但这感觉像是一种hack。我认为在不适用的情况下显示/隐藏菜单并不是良好的用户体验,我宁愿在不适用的情况下禁用它们。这样用户就知道有一个菜单,但它当前并不适用于这种情况。

import QtQuick
import QtQuick.Controls
import QtQuick.Window

ApplicationWindow {
    id: window
    width: 320
    height: 260
    visible: true

    menuBar: MenuBar {
        Menu {
            id: menu1
            title: qsTr("&File")
            Action { text: qsTr("&New...") }
            Action { text: qsTr("&Open...") }
            Action { text: qsTr("&Save") }
            Action { text: qsTr("Save &As...") }
            MenuSeparator { }
            Action { text: qsTr("&Quit") }
        }
        Menu {
            id: menu2
            title: qsTr("&Edit")
            Action { text: qsTr("Cu&t") }
            Action { text: qsTr("&Copy") }
            Action { text: qsTr("&Paste") }
        }
        Menu {
            id: menu3
            title: qsTr("&Help")
            Action { text: qsTr("&About") }
        }

        onMenusChanged: checkMenu()
        Component.onCompleted: checkMenu()

        function checkMenu() {
            if (window.menuBar.menuAt(1) === menu2) {
                removeButton.enabled = true
                addButton.enabled = false
            } else {
                removeButton.enabled = false
                addButton.enabled = true
            }
        }
    }

    Column {
        anchors.centerIn: parent
        spacing: 6

        Button {
            id:  removeButton
            text: "Remove"
            onClicked: {
                window.menuBar.takeMenu(1)
            }
        }

        Button {
            id: addButton
            text: "Add"
            onClicked: {
                window.menuBar.insertMenu(1, menu2)
            }
        }
    }
}
英文:

You can use takeMenu() and insertMenu(), but this feels like a hack. In my opinion it isn't good UX to show/hide menus I would rather disable them if not applicable. This way the user knows there is a menu, but it currently doesn't apply to the situation.

import QtQuick
import QtQuick.Controls
import QtQuick.Window
ApplicationWindow {
id: window
width: 320
height: 260
visible: true
menuBar: MenuBar {
Menu {
id: menu1
title: qsTr("&File")
Action { text: qsTr("&New...") }
Action { text: qsTr("&Open...") }
Action { text: qsTr("&Save") }
Action { text: qsTr("Save &As...") }
MenuSeparator { }
Action { text: qsTr("&Quit") }
}
Menu {
id: menu2
title: qsTr("&Edit")
Action { text: qsTr("Cu&t") }
Action { text: qsTr("&Copy") }
Action { text: qsTr("&Paste") }
}
Menu {
id: menu3
title: qsTr("&Help")
Action { text: qsTr("&About") }
}
onMenusChanged: checkMenu()
Component.onCompleted: checkMenu()
function checkMenu() {
if (window.menuBar.menuAt(1) === menu2) {
removeButton.enabled = true
addButton.enabled = false
} else {
removeButton.enabled = false
addButton.enabled = true
}
}
}
Column {
anchors.centerIn: parent
spacing: 6
Button {
id:  removeButton
text: "Remove"
onClicked: {
window.menuBar.takeMenu(1)
}
}
Button {
id: addButton
text: "Add"
onClicked: {
window.menuBar.insertMenu(1, menu2)
}
}
}
}

huangapple
  • 本文由 发表于 2023年7月27日 22:26:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定