Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt5: How to hide or remove a QMenu from the QMenuBar?

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);
}
like image 477
סטנלי גרונן Avatar asked Jan 16 '16 10:01

סטנלי גרונן


People also ask

How do you hide Qmenu?

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.

How do I add menu bar to QT?

In most main window style applications you would use the menuBar() function provided in QMainWindow, adding QMenus to the menu bar and adding QActions to the pop-up menus. Example (from the Menus example): fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAct);


1 Answers

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.

like image 189
Evgeny Avatar answered Oct 26 '22 23:10

Evgeny