QT How to remove the action menu item

11,348

Solution 1

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);

Solution 2

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.

Solution 3

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)

Share:
11,348
Naruto
Author by

Naruto

Updated on June 04, 2022

Comments

  • Naruto
    Naruto about 2 years

    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

  • Naruto
    Naruto over 14 years
    Ok.. how to get the acces to qaction menu item?.. i will be getting the actions list from the menu. if i iterate over it doesn't gives Action menu only.. so what to do?
  • Patrice Bernassola
    Patrice Bernassola over 14 years
    What do you want to exactly do?
  • Naruto
    Naruto over 14 years
    i want to remove the Qaction menu from menubar.. so is there any way? for making set-visible we need to get access for it.. we are not getting only how to do?
  • Patrice Bernassola
    Patrice Bernassola over 14 years
    See Edit. But I'm not sure to understand the goal of your work. Are you trying to dynamically display QActions in the menuBar?
  • Naruto
    Naruto over 14 years
    No.. i just want to hide the Action's name from menu item at runtime.. "actiontest" represet wat in you edited section?. "My goal is to hide the the default action name from menu item at runtime"
  • Patrice Bernassola
    Patrice Bernassola over 14 years
    actionTest is a QAction you have created using QtDesigner or you have manually added (in the constructor by example)
  • Naruto
    Naruto over 14 years
    i am using the constructor to create menu.. see in constructor how i am doing.. i have put up code in my quest above
  • Patrice Bernassola
    Patrice Bernassola over 14 years
    You have to keep a trace of your actions if you want to use it later as you want. Put verAction, displayAction and exitAction as member of your class so you have access to them at any time (don't forget that ownership stays to the menu bar and you must not delete them). An other way is to find a child of the menu bar regarding its name. Read this doc.trolltech.com/qq/qq03-big-brother.html#finding.children