Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to auto display shortcut key in context menu like QActions in menuBar?
Forum Updated to NodeBB v4.3 + New Features

How to auto display shortcut key in context menu like QActions in menuBar?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    alt text

    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));
    

    https://github.com/sonichy

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should set the action's shortcutVisibleInContextMenu property.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by sonichy
        #3

        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);
        }
        

        https://github.com/sonichy

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved