Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

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

    General and Desktop
    qmenubar
    2
    6
    1620
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      detly last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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
        

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply Reply Quote 1
        • D
          detly @SGaist last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply Reply Quote 0
            • D
              detly @SGaist last edited by

              @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?

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post