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

青春壹個敷衍的年華 提交于 2019-12-05 19:49:49

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?

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.

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