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

[Solved] QMenuBar && menu's order



  • Hi all,
    I have a QMainWindow with it's menubar.
    In the mainWindow contructor I add two menus (File, help) .

    When users login in a QDialog I need to add some other menus in the menubar after the "File" menu and before the "Help" menu.

    Ho can I do without deleting and re-adding all menus?



  • Well, you could always try QMenuBar::insertMenu()...



  • Thanks Andre,
    I didn't see insertMenu(). I was looking for addSomething(..., int pos) ... :-)



  • This is a pattern in Qt. In the Qt API, add appends at the end, and insert somewhere in the middle. Keep an eye out for it, you'll spot it everywhere it makes sense :-)



  • I'm trying
    @
    QMenuBar::insertMenu ( QAction * before, QMenu * menu )
    @

    but I don't undertand very well what is :
    @
    QAction * before
    @

    I need to insert the new menu after or before another menu in the menubar, not before an action...



  • insertMenu() and addMenu() return a QAction pointer which can be used as the first argument. You will have to save the pointer though.



  • Ok, I solved inserting the "Help" menu too with insertMenu() instead of addMenu() so I get a QAction that I can use for all the menus I must insert before "Help".

    Thanks.


Log in to reply