Naruto
Naruto

Reputation: 9634

QT How to remove the action menu item

when i add the widget to the main window, by default the action menu item will be present, how to remove that?

menuBar()->setVisible(false);

verAction = new QAction(tr("&Version"),this);
menuBar()->addAction(verAction);
connect(verAction, SIGNAL(triggered()),this, SLOT(displayVersion()));

displayAction = new QAction(tr("&Display"),this);
menuBar()->addAction(displayAction);


 connect(displayAction, SIGNAL(triggered()),this, SLOT(displayMessage()));

exitAction = new QAction(tr("&Exit"),this);
menuBar()->addAction(exitAction);
connect(exitAction, SIGNAL(triggered()),this, SLOT(close()));

Thanks

Upvotes: 4

Views: 12710

Answers (3)

Mircea
Mircea

Reputation: 61

In order to remove the default context menu with the label "Actions" the following code may be used:

// Remove context menu from the all widgets.
QWidgetList widgets = QApplication::allWidgets();
QWidget* w=0;
foreach(w,widgets) {
    w->setContextMenuPolicy(Qt::NoContextMenu);
}

Essentially, the same as the Joel's answer, but the code version :)

(Code taken from QFriendFeed sample from forum.nokia.com)

Upvotes: 2

Joel
Joel

Reputation: 21

I know what you mean... you want to HIDE the DEFAULT CONTEXT MENU "Actions"....

You can do this in the Design section (not in code).

Then you see your Object-Stack on the right side like

  • MainWindow QMainWindow
    • centralWidget QWidget
      • webView QWebView

Now go to the property editor below...search for "contextMenuPolicy" and change it from "DefaultContextMenu" to "NoContextMenu" for every component if necessairy.

Upvotes: 2

Patrice Bernassola
Patrice Bernassola

Reputation: 14416

If you want to hide an QAction and display it when you need it, you can use the setVisible function.

If you want to remove the menu bar from the QMainWindow, you can use the QT_NO_MENUBAR preprocessor to remove all uses of a QMenuBar. If you are not using facilities provided by QMainWindow, maybe you can use a simple QWidget as main window in your application.

[Edit] If you want to hide QActions at runtime, you will find them as member of the QMainWindow's UI. For example if you have a QAction named actionTest, you will access it like that: this->ui->actionTest->setVisible(false);

Upvotes: 2

Related Questions