Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What's the practical/standard way to share a QMenuBar for the whole application?



  • Hi,

    Title. I want a single QMenuBar to be shared by all widgets/windows in my application, like a macOS menu bar (see What's the cross-platform way to create a QMenuBar?). In this menu bar, QActions should be enabled or disabled depending on the active window. What's the standard way to do this and where can I learn more about this? I fill a bit lost with this.

    Thanks a lot and let me know if I can help in any way.


  • Lifetime Qt Champion

    Hi,

    AFAIK, there's no standard way. But personally I would find it strange that the QMenuBar content changes when I switch from one widget to another.

    It's rather something I would expect from a QToolBar associated with the widget.

    Can you explain your use case ?


  • Lifetime Qt Champion

    Hi,

    AFAIK, there's no standard way. But personally I would find it strange that the QMenuBar content changes when I switch from one widget to another.

    It's rather something I would expect from a QToolBar associated with the widget.

    Can you explain your use case ?



  • Thanks. If you know of certain ways to do it, I would very much appreciate a link to an example or resources.

    I was not clear: the content of the menu bar does not change; QMenus and QActions are always the same. Rather, the fact that a given QAction is enabled or not changes with the windows.

    I do not like tool bars very much because they become very bloated when you have a lot of options. Also, menu bars make organizing stuff easy. My use case is the following: I have a main window and auxiliary windows (preferences widget, etc). Actions enabled for the main window should not be for the auxiliary windows.

    I want the menu bar to stay the same everywhere because I feel like having different menu bars makes it hard to remember where is each option (e.g. in the software Anki).

    EDIT: And how can you have a QMenuBar if you don't have any QMainWindow on screen?


  • Lifetime Qt Champion

    For the action state, you can use the enter/leaveEvent method for example.

    Depending on how these widgets are managed, you may also tie the action enabled state to the widget visibility.

    Which OS are you targeting ?



  • Ok, I will keep this in mind and so some research.

    We target Linux, macOS and Windows!


  • Lifetime Qt Champion

    @w3bvkas9 said in What's the practical/standard way to share a QMenuBar for the whole application?:

    EDIT: And how can you have a QMenuBar if you don't have any QMainWindow on screen?

    QMenuBar is a widget that you can use with other widgets. QMainWindow has it integrated.



  • Thanks. I think you answered the question, please feel free to add anything later if you want. I mark it as solved.