Qt creator, paste the menu at the specified location in the menu bar

I created a menu bar and some menus with the creator of Qt. One of the menus had to be encoded to use the QActionGroup functions. Now it's easy to add my menu to the menu bar with

 printMenu = menuBar()->addMenu(tr("&Print")); 

but my menu will be at the last position of the menu bar. How to add a menu to a specified location? (for example, second place immediately after the File menu)

Hi

+4
source share
2 answers

Use QMenuBar :: insertMenu in combination with QMenu :: menuAction .

For example, if you want to dynamically insert the Print menu in the space in front of the Help menu, you can do something like this:

 QMenu *printMenu = new QMenu(tr("&Print")); menuBar()->insertMenu(ui->menuHelp->menuAction(), printMenu); 
+8
source

If you want to add a submenu in the middle of the menu bar, this is not trivial. There is no direct API for this, but you can probably disable this byt by controlling the internal actions of QWidget (QMenu :: addMenu just calls QWidget::addAction(menu->menuAction()) .

In theory, you can manipulate QMenuBar :: actions (), but I never did that.

When I had to deal with this problem, I just restored the menu from another data set (look at your favorite search engine for qmdilib and you will see my solution).

+1
source

Source: https://habr.com/ru/post/1312852/


All Articles