qtqtableviewqmenuqaction

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:

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

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.

Qt 5.12. Linux (KDE Neon)

Edit: Here is code which rise popup

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

void UserPlaylistsSubWidget::popUp(const QPoint &pos)
{
    mPopup.popup(ui->list->viewport()->mapToGlobal(pos));
}

Solution

  • 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);
    a->setShortcut(QKeySequence(Qt::Key_F5));
    a->setShortcutVisibleInContextMenu(true);
    connect(a, &QAction::triggered, this, &UserPlaylistsSubWidget::refreshList);
    ui->list->addAction(a);