英文:
QML: How to hide a Menu item from the ApplicationWindow MenuBar
问题
在QML/Qt6.5中,我尝试在ApplicationWindow
的MenuBar
中动态添加/移除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)
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论