Hiding Menu in MainWindow



  • I would like to hide one of my menu's in the main window in case it has no actions (this depends on whether certain plugins are loaded or not). Calling setVisible(false) on the QMenu does not hide it. So, what is the correct approach for this case?



  • @ModelTech
    Can you show what you have tried?


  • Moderators

    What kind of menu is it? A context menu? On a menu bar? On a toolbar's action? On a button?
    If it's a menu added to a menu bar you should hide the action returned from addMenu(), not the menu itself.



  • It's the menu of the QMainWindow. This is how I created the QMenu with QMainWindow::menuBar()

    AnalysisMenu = menuBar()->addMenu(tr("Analysis"));
    

    Subsequently, I have some code that may or may not add QActions to this AnalysisMenu. For this I use QMenu::addAction. Finally, I have tried the following to hide the menu in case it does not have actions:

    AnalysisMenu->setEnabled(!AnalysisMenu->actions().isEmpty());
    

  • Moderators

    setEnabled does not hide an action. It only disables it (grays out depending on the style) and turns off a shortcut if it was assigned. Use setVisible() instead.



  • Oeps, that's my current code... I actually did use setVisible as follows, but it does not give the desired effect:

    AnalysisMenu->setVisible(!AnalysisMenu->actions().isEmpty());
    

  • Moderators

    If it doesn't work it means you're either not calling this code or you call it in a wrong place. It should be called whenever you change the contents of the menu (add/remove actions).



  • Have now done so, but that does not help... I am using Qt 5.7.1 if that matters.


  • Moderators

    You'll gonna have to show some code - how you set up the menu, the part where you modify it and where you call the line. setVisible(false) hides an action so the problem is somewhere in your code.



  • I just found out that the problem is platform related. On MacOS, it works as I expect. So, will dive a bit deeper ;)



  • Yep, it also works correctly on Windows. It just does not work correctly on Ubuntu. It seems related to a menu problem that I experienced before on Ubuntu only. So, I guess that I will have to wait for a bug fix...


Log in to reply
 

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