Setting Custom Position of QMenuBar inside of MainWindow



  • Hello friends,

    I have a frameless/borderless MainWindow. It is only a white window w/borders.

    When I add a QMenuBar to the MainWindow (via this->setMenuBar() ) , by default the QMenuBar is placed at the very top of the window, and I see no provided function to set a different position. Using ->setGeometry() does not work as it does w/other widgets.

    I do not want my QMenuBar all the way at the top, I want it much lower. So how can I add a custom position of my QMenuBar within my MainWindow? Can someone please give me a brief explanation/code snippet?

    Thank you very much for your time and concern. Have a great day.



  • Hi and welcome to devnet,

    menubar position is handled automatically by the windows system (for example in OS X the menubar is on top of the screen and not inside the application window).

    For this reason I think you cannot control its position. If you want to do this you MUST implement your own widget (not a QMainWIndow)



  • Thanks for your answer mcosta, I appreciate it.

    In my humble opinion, I think that's really bad because on border/frameless windows you have to manually draw exit, minimize/maximize, etc buttons in the top right corner and if the QMenuBar is already there - well, you can't.

    I guess I could make a QGraphicsView/Scene, then add it to the scene, but then it would be a ton of code to make everything resize properly when the window is manually resize via mouse drags.

    I may just end up switching back to WinAPI because I think natively (in windows only) handling frameless/borderless windows WinAPI is just better at it. Microsoft provides several code examples for this and shows how to easily customize it.

    So I don't think what I am trying is all the complicated but surely there must be a cleaner solution than doing it in my own widget from scratch or reimplementing my whole program in a graphics view right?

    Once again, thank you very much for your time and concern and have a great day.



  • Have you tried adding your QMenuBar object to your QMainWindow's layout just like an ordinary widget? Unlike using setMenuBar(), you will have padding around it unless you modify your MainWindow's stytlesheet (I think).



  • @mchinand No I sure haven't, would you mind supplying a brief code snippet/link showing what that looks like in code? I've tried adding widgets to MainWindow but there is no addWidget() member like w/GraphicViews, etc. Thank you very much for your help.



  • @ronaroja Check out the Menus example. This creates a QWidget that is used as the application central widget. Widget are added to the QVBoxLayout of this central widget. You will have to modify this example slightly; don't use the setMenuBar() and create a QMenuBar widget that you can add to the QVBoxLayout (doesn't have to be at the top).



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