当前位置: 代码迷 >> QT开发 >> 怎么设置菜单快捷键
  详细解决方案

怎么设置菜单快捷键

热度:76   发布时间:2016-04-25 05:03:45.0
如何设置菜单快捷键
如果我希望有一个QMenu,显示字串是“文件”,快捷键是“Ctrl+F”.
使用代码能实现这样一个菜单么?该怎么作?

------解决方案--------------------
QAction *act = new QAction(QString::fromLocal8Bit("文件"));
act->setShortcut(tr("Ctrl+F"));
------解决方案--------------------
menu一般用 Alt+XXX

new QMenu("&File", this);

非要用Ctrl+F就在keyPressedEvent里抓吧。。
------解决方案--------------------
menu->addAction(...);

void QMenu::addAction ( QAction * action )
Appends the action action to the menu's list of actions.

void QMenuBar::addAction ( QAction * action )
Appends the action action to the menu bar's list of actions.
------解决方案--------------------
//class define///////////// 
QMenu *fileMenu;
QAction *newAction;
///////////////////////////

//void createAction();////
newAction = new QAction(tr("&New"), this);
newAction->setShortCut(tr("Ctrl+F")); 
//or (newAction->setShortCut(QKeySequence::New));)
//////////////////////////

//void createMenu();//////
fileMenu = MenuBar()->addMenu(tr("File"));
fileMenu->addAction(newAction)
//////////////////////////
  相关解决方案