I am developing an application in Qt that rebuilds its menus very often. However, when we call clear(), and re-add the actions that we want in the menu, "Special Characters..." appears to remain in the menu. Is there any way to remove, or move this action to the bottom of the QMenu?
Here is the code that rebuilds the menu:
void MainWindow::initMenus(Tab* tab)
{
menuBar()->clear();
menuFile->clear();
menuEdit->clear();
menuSettings->clear();
menuHelp->clear();
ui_toolBar->clear();
menuBar()->addMenu(menuFile);
menuBar()->addMenu(menuEdit);
menuFile->addAction(actionNew);
menuFile->addAction(actionOpen);
if(tab) tab->addActionsFile(menuFile);
menuFile->addSeparator();
menuFile->addAction(actionNext);
menuFile->addAction(actionPrevious);
menuFile->addAction(actionClose);
menuFile->addSeparator();
menuFile->addAction(actionQuit);
if(tab) {
tab->addActionsEdit(menuEdit);
menuEdit->addSeparator();
tab->addActionsHelp(menuHelp);
menuHelp->addSeparator();
}
menuEdit->addAction(actionEditor_Settings);
menuHelp->addSeparator();
menuHelp->addAction(actionAbout);
if(tab) tab->addOtherActions(menuBar());
menuBar()->addMenu(menuHelp);
ui_toolBar->addAction(actionNew);
ui_toolBar->addAction(actionOpen);
if(tab) tab->addToolbarActions(ui_toolBar);
}
It is supplied a tab, which can add its own actions to the menu as well using those functions.
This answer may be more applicable to a COCOA OSX app, but I was able to remove these menu items in Objective-C by getting an NSMenu handle to the Edit Menu itself in the applicationDidFinishLaunching function , looping through the NSMenuItems in the itemArray and removing them by calling removeItem.
This is a feature of Mac OS X that isn't easily disabled. You'll notice that nearly every application on Mac OS has it. It's automatically added to the Edit menu by the OS to allow the input of international characters.
It seems from your question, but isn't quite clear, that when you initially create the Edit menu, the Special Characters... menu item is initially the last menu item, but becomes the first menu item once
editMenu->clear()
has been called. One route you could go is instead ofclear()
ing the menus, you candelete
the menus and recreate them completely. Your edit menu looks pretty static, though. Maybe it doesn't have to be recreated at all.Now, that said, if you're really sure you need to get rid of this menu item, there are a couple of ways to accomplish that.
The first, and least desirable one is to simply not have an "Edit" menu. If there's no menu titled "Edit", Mac OS will not add a "Special Characters" menu item.
The second method requires a little platform specific Objective-C code. Obviously, this should only be built into your project on Mac OS.
MenuDeleter.m:
MenuDeleter.h
And finally, in main.cpp:
So that's how to make it go away completely. But the question is, do you really want to prevent the user from being able to input special characters into your app?