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

2019-05-11 06:06发布

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.

Special Characters Option in Edit Menu

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-05-11 06:47

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.

查看更多
我只想做你的唯一
3楼-- · 2019-05-11 06:55

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?

查看更多
登录 后发表回答