Solved How to auto display shortcut key in context menu like QActions in menuBar?
-
QMenu *menu = new QMenu; menu->setStyleSheet("color:rgb(255,255,255); background:rgba(0,0,0,100);"); menu->setAttribute(Qt::WA_TranslucentBackground, true); menu->setAutoFillBackground(true); QAction *action_brush = new QAction(QIcon(":/icons/brush.png"), "画笔 1", this); action_brush->setShortcut(QKeySequence(Qt::Key_1)); QAction *action_line = new QAction(QIcon(":/icons/line.svg"), "直线 2", this); action_line->setShortcut(QKeySequence(Qt::Key_2)); QAction *action_ellipse = new QAction(QIcon(":/icons/ellipse.svg"), "椭圆 3", this); action_ellipse->setShortcut(QKeySequence(Qt::Key_3)); QAction *action_rect = new QAction(QIcon(":/icons/rect.svg"), "方框 4", this); action_rect->setShortcut(QKeySequence(Qt::Key_4)); QAction *action_stamp = new QAction(QIcon(":/icons/image.svg"), "图片 5", this); action_stamp->setShortcut(QKeySequence(Qt::Key_5)); QAction *action_change_stamp = new QAction(QIcon(":/icons/image.svg"), "换图 6", this); action_change_stamp->setShortcut(QKeySequence(Qt::Key_6)); QAction *action_change_color = new QAction(QIcon(":/icons/color.svg"), "换色 7", this); action_change_color->setShortcut(QKeySequence(Qt::Key_7)); QAction *action_quit = new QAction(QIcon(":/icons/quit.svg"), "退出 Ctrl + Q", this); action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); menu->addAction(action_brush); menu->addAction(action_line); menu->addAction(action_ellipse); menu->addAction(action_rect); menu->addAction(action_stamp); menu->addAction(action_change_stamp); menu->addAction(action_change_color); menu->addAction(action_quit); ui->pushButton_menu->setMenu(menu); ui->pushButton_menu->setShortcut(QKeySequence(Qt::Key_M));
-
Hi,
You should set the action's shortcutVisibleInContextMenu property.
-
Thank you !
// one by one QAction *action_brush = new QAction(QIcon(":/icons/brush.png"), "画笔", this); action_brush->setShortcut(QKeySequence(Qt::Key_1)); action_brush->setShortcutVisibleInContextMenu(true); // many foreach(QAction *action, menu->actions()){ action->setShortcutVisibleInContextMenu(true); }