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

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

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

问题

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

  1. ApplicationWindow
  2. {
  3. id: mainWindow
  4. width: 600
  5. height: 400
  6. visible: true
  7. menuBar: MenuBar {
  8. id: mainMenuBar
  9. // this item should be shown/hidden at runtime
  10. Menu {
  11. id: subMenu
  12. title: "Menu to hide"
  13. MenuItem {
  14. text: "Menu Item"
  15. }
  16. }
  17. }
  18. }

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

将高度设置为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.

  1. ApplicationWindow
  2. {
  3. id: mainWindow
  4. width: 600
  5. height: 400
  6. visible: true
  7. menuBar: MenuBar {
  8. id: mainMenuBar
  9. // this item should be shown/hidden at runtime
  10. Menu {
  11. id: subMenu
  12. title: "Menu to hide"
  13. MenuItem {
  14. text: "Menu Item"
  15. }
  16. }
  17. }
  18. }

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。我认为在不适用的情况下显示/隐藏菜单并不是良好的用户体验,我宁愿在不适用的情况下禁用它们。这样用户就知道有一个菜单,但它当前并不适用于这种情况。

  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtQuick.Window
  4. ApplicationWindow {
  5. id: window
  6. width: 320
  7. height: 260
  8. visible: true
  9. menuBar: MenuBar {
  10. Menu {
  11. id: menu1
  12. title: qsTr("&File")
  13. Action { text: qsTr("&New...") }
  14. Action { text: qsTr("&Open...") }
  15. Action { text: qsTr("&Save") }
  16. Action { text: qsTr("Save &As...") }
  17. MenuSeparator { }
  18. Action { text: qsTr("&Quit") }
  19. }
  20. Menu {
  21. id: menu2
  22. title: qsTr("&Edit")
  23. Action { text: qsTr("Cu&t") }
  24. Action { text: qsTr("&Copy") }
  25. Action { text: qsTr("&Paste") }
  26. }
  27. Menu {
  28. id: menu3
  29. title: qsTr("&Help")
  30. Action { text: qsTr("&About") }
  31. }
  32. onMenusChanged: checkMenu()
  33. Component.onCompleted: checkMenu()
  34. function checkMenu() {
  35. if (window.menuBar.menuAt(1) === menu2) {
  36. removeButton.enabled = true
  37. addButton.enabled = false
  38. } else {
  39. removeButton.enabled = false
  40. addButton.enabled = true
  41. }
  42. }
  43. }
  44. Column {
  45. anchors.centerIn: parent
  46. spacing: 6
  47. Button {
  48. id: removeButton
  49. text: "Remove"
  50. onClicked: {
  51. window.menuBar.takeMenu(1)
  52. }
  53. }
  54. Button {
  55. id: addButton
  56. text: "Add"
  57. onClicked: {
  58. window.menuBar.insertMenu(1, menu2)
  59. }
  60. }
  61. }
  62. }
英文:

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.

  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtQuick.Window
  4. ApplicationWindow {
  5. id: window
  6. width: 320
  7. height: 260
  8. visible: true
  9. menuBar: MenuBar {
  10. Menu {
  11. id: menu1
  12. title: qsTr("&File")
  13. Action { text: qsTr("&New...") }
  14. Action { text: qsTr("&Open...") }
  15. Action { text: qsTr("&Save") }
  16. Action { text: qsTr("Save &As...") }
  17. MenuSeparator { }
  18. Action { text: qsTr("&Quit") }
  19. }
  20. Menu {
  21. id: menu2
  22. title: qsTr("&Edit")
  23. Action { text: qsTr("Cu&t") }
  24. Action { text: qsTr("&Copy") }
  25. Action { text: qsTr("&Paste") }
  26. }
  27. Menu {
  28. id: menu3
  29. title: qsTr("&Help")
  30. Action { text: qsTr("&About") }
  31. }
  32. onMenusChanged: checkMenu()
  33. Component.onCompleted: checkMenu()
  34. function checkMenu() {
  35. if (window.menuBar.menuAt(1) === menu2) {
  36. removeButton.enabled = true
  37. addButton.enabled = false
  38. } else {
  39. removeButton.enabled = false
  40. addButton.enabled = true
  41. }
  42. }
  43. }
  44. Column {
  45. anchors.centerIn: parent
  46. spacing: 6
  47. Button {
  48. id: removeButton
  49. text: "Remove"
  50. onClicked: {
  51. window.menuBar.takeMenu(1)
  52. }
  53. }
  54. Button {
  55. id: addButton
  56. text: "Add"
  57. onClicked: {
  58. window.menuBar.insertMenu(1, menu2)
  59. }
  60. }
  61. }
  62. }

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:

确定