12

I have built up a QMenu MainMenu on top of my MainWindow in my application. As everybody is used to it, I have following QMenu Main menus:

File - Edit - SuperHeavyExpertMenus - Settings - Help

I would like to hide the sub tree SuperHeaverExpertMenus initially, on program start, because it conatins a lot of settings, which might confuse beginner users.

I want to show that sub tree with SuperHeavyExpertMenu, when a checkbox in Settings is true, for instance.

I can hide the children QActions of the QMenu s above, vie QAction:setVisible(false) . but I can do that for the QMenu ?

Is there a way to hide the QMenu ?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103
  • 1
    Hide [Menu action](http://doc.qt.io/qt-5.5/qmenu.html#menuAction). – Amartel Sep 03 '15 at 12:33
  • @Amartel I can not see a possiblity to hi hide a QMenu with that? I am not interesetes in the QActions. I am already able to hide the QActions. But the QMenu are still visible in the main menu. I want to hide a QMenu. – Ralf Wickum Sep 03 '15 at 12:39
  • @RalfWickum In this case the word "action" just means "thing that can go in a menu". From the QMenu description: *"There are four kinds of action items: separators, actions that show a submenu, widgets, and actions that perform an action."* SuperHeavyExpertMenus would be an "action", of the second kind. – HostileFork says dont trust SE Sep 03 '15 at 12:44
  • Not "menu's actions". **Menu action**. I posted a link to a documentation. Follow it. – Amartel Sep 03 '15 at 13:03
  • 1
    Yes, actually, myMenu->menuAction()->setvisible(false) solved my problem. – Ralf Wickum Sep 04 '15 at 09:39

3 Answers3

27

If I understood correctly your problem, the way you can solve it is whit something like this:

ui->menuYouWantToHide->menuAction()->setVisible(false);

I hope to be useful.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
3

Qmenu doesn't have setVisible(), or hide(), but you can change the title instead:

ui->MenuYouWantToHide->setTitle("");

This will remove the title, and therefore the "Action" that make up the button in your GUI.

barbsan
  • 3,418
  • 11
  • 21
  • 28
Tomse
  • 179
  • 5
  • Indeed when you do: **menu->setTitle("")** it's like **menu->hide()**. Then when you set a title later the menu is displayed with all actions within. – Patapoom Jun 12 '19 at 12:15
  • @Patapoom only difference that it is still "visible", which may affect some logic relying on it. Note, that it wouldn't disable shortcut key associated with same action. – Swift - Friday Pie May 08 '22 at 13:05
0

Visibility and enable-ity of QAction-based widgets can be controlled via QAction object connected with them.

Qt documentation, even latest versions omit multiple accessor functions from list of public ones. Instead they are referenced under relevant getters, e.g. QAction::setVisible is listed under QAction::isVisible topic.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42