7

I am using Qt5 on Windows7 platform:
Qt Creator version is: v3.3.2.
Qt version 5.5.1 and MinGW 32bit.

Currently, in the menu bar I have:

Configuration - Reports - Help

I searched SO and I found this as a possible answer: Not possible to hide a QMenu object QMenu::setVisible()?, but it didn't work...

So, I was trying to remove the Help menu using:

ui->menuHelp->setVisible(false);

and:

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

Unfortunatelly, both failed to hide/remove the Help menu...

Please, is there any other way to do it?

[Code]:

MainWindow::MainWindow(QWidget * parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlags(this->windowFlags() & ~Qt::WindowMaximizeButtonHint);
    if(!server.listen(QHostAddress("192.168.1.2"), 8001))
        return;
    if(true) // just testing...
       ui->menuHelp->menuAction()->setVisible(false);
}
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
  • 1
    Just for test I've added 3 menu to menuBar. Then I try this `ui->menu3->menuAction()->setVisible(false);` and it work like expected - hides `menu3`. Your promblem is somewhere else. – Evgeny Jan 16 '16 at 14:43
  • Did you use Qt Creator Designer? Then you tried `setVisible(false)` from within the `MainWindow`'s constructor or where exactly?... What am I doing wrong? – סטנלי גרונן Jan 16 '16 at 15:21
  • I do this from Window constructor, yes. And I created form with Designer. Show more your code – Evgeny Jan 16 '16 at 15:26
  • Now I see why you said "...the problem is somewhere else". I guess I should put some debug to check whether the server fails or not to listen on port 8001. If it fails, then obviously it returns immediately and of course the Help menu won't be hidden... – סטנלי גרונן Jan 16 '16 at 15:59
  • Yes, you were right! Server failed to listen on that port/IP... Yet, out of curiosity: what exactly is this line of code suppose to do: `ui->menuHelp->setVisible(false);`? It compiles ok as a matter of fact, but it doesn't hide anything :) – סטנלי גרונן Jan 16 '16 at 16:05
  • I have added explanation as regular answer. Please accept it. – Evgeny Jan 16 '16 at 16:16

1 Answers1

17

Just for test, I've added 3 menus to menuBar.
Then I tried this:

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

And it worked like expected - it hides menu3. Your problem is somewhere else.

The code ui->menuHelp->setVisible(false); hides the menu, not the action on menuBar. For example when you click on action on menuBar the menu becomes visible. Then you can hide it with this line of code. But when you call this directly from constructor the menu is still invisible, so this code does nothing.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Evgeny
  • 3,910
  • 2
  • 20
  • 37