Qt Mac (Re)move “Special Characters…” action in Edit menu

纵饮孤独 提交于 2019-12-22 10:17:15

问题


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.


回答1:


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 of clear()ing the menus, you can delete 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:

#include <Foundation/NSUserDefaults.h>

void deleteSpecialCharacters()
{
    [[NSUserDefaults standardUserDefaults]
        setBool:YES forKey:@"NSDisabledCharacterPaletteMenuItem"];
}

MenuDeleter.h

#ifndef MENUDELETER_H_
#define MENUDELETER_H_

void deleteSpecialCharacters();

#endif

And finally, in main.cpp:

#include <QApplicaiton>
#include "MenuDeleter.h"

int main(int argc, char **argv)
{
#ifdef Q_OS_MAC
    deleteSpecialCharacters();
#endif
    QApplication app(argc, argv);
    ....
    return app.exec();
}

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?




回答2:


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.



来源:https://stackoverflow.com/questions/6391053/qt-mac-remove-special-characters-action-in-edit-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!