What's the cross-platform way to create a QMenuBar?



  • In the documentation for QMainWindow::MenuBar it says:

    If you want all windows in a Mac application to share one menu bar, don't use this function to create it, because the menu bar created here will have this QMainWindow as its parent. Instead, you must create a menu bar that does not have a parent, which you can then share among all the Mac windows.

    But if I'm using Qt5 to write a cross platform application, I want my menu bar to appear properly on all desktop environments. On OS X I want the shared menu, on Linux, Windows and others I want the menubar in the main window.

    How can I achieve this?


  • Lifetime Qt Champion

    Hi,

    Do you have that multiple window sharing the same QMenuBar use case ? If so just use an ifdef for OS X i and use the code shown in QMenuBar's documentation i.e.:

    #ifdef Q_OS_OSX
    QMenuBar *menuBar = new QMenuBar(nullptr);
    #endif
    


  • @SGaist said:

    Do you have that multiple window sharing the same QMenuBar use case?

    No, in my case it's a single window. But it's still idiomatic to have the menubar at the top of the screen of OS X, even for a single-window app.

    Your #ifdef approach should work, cheers.


  • Lifetime Qt Champion

    You may have misunderstood something then. That technique is to be used if you want to have all windows of your application use the same menu bar. However it won't change de position of the menu bar.

    Additionally, you shouldn't try to have that menu bar inside your main window because you'll be working against the OS X Human Interface Guidelines and you'll alienate your OS X users. Qt follows the platform recommendations for a good reason: the application should look native so that user of said platform will feel at home with your application.



  • @SGaist said:

    Additionally, you shouldn't try to have that menu bar inside your main window because you'll be working against the OS X Human Interface Guidelines and you'll alienate your OS X users. Qt follows the platform recommendations for a good reason: the application should look native so that user of said platform will feel at home with your application.

    Yes! This is what I want to achieve. But the docs imply that you need one line of code for OS X, and another for everything else. So... what's the correct way?


  • Lifetime Qt Champion

    If you mean that you want to follow the guidelines then you have nothing special to do, just use menuBar() and you're good to go.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.