QMenu - shortcut is not triggered

  • What is correct way to use QAction shortcuts? I have QTableView with custom context menu where beside other actions I want to have action Refresh F5

    QAction *a;
    a = mPopup.addAction(IconsManager::icon(fa::refresh), "Refresh", this, &UserPlaylistsSubWidget::refreshList, QKeySequence(Qt::Key_F5));

    First, I had to set setShortcutVisibleInContextMenu to make it visible in context menu but action is still not triggered when press F5 (QTableView is active and focused widget). Tried also different values for QAction::setShortcutContext but still no result.
    Here is code which rise popup:

    connect(ui->list, &QWidget::customContextMenuRequested, this, &UserPlaylistsSubWidget::popUp);
    void UserPlaylistsSubWidget::popUp(const QPoint &pos)

  • Lifetime Qt Champion


    Out of curiosity, why is it a shortcut specific to a contextual menu ? Looks like it would make more sense as a general action.

  • Figured it out. Didn't know that QTableView has own actions list and can show it in own popup with setContextMenuPolicy(Qt::ActionsContextMenu). So here is correct solution and F5 shortcut works as expected:

    QAction *a = new QAction(IconsManager::icon(fa::refresh), "Refresh", ui->list);
    connect(a, &QAction::triggered, this, &UserPlaylistsSubWidget::refreshList);

  • Lifetime Qt Champion

    Side note: you can add actions to all QWidget based classes.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.