使用QAction快捷方式的正确方法是什么?我有自定义上下文菜单的QTableView,除了我想要有动作的其他动作Refresh F5
:
// Popup
QAction *a;
a = mPopup.addAction(IconsManager::icon(fa::refresh), "Refresh", this, &UserPlaylistsSubWidget::refreshList, QKeySequence(Qt::Key_F5));
a->setShortcutVisibleInContextMenu(true);
首先,我必须设置setShortcutVisibleInContextMenu
以使其在上下文菜单中可见,但是当按F5(QTableView处于活动状态且聚焦小部件)时仍未触发操作。尝试了QAction::setShortcutContext
的不同值,但仍然没有结果。
Qt 5.12。 Linux(KDE)
编辑:这是弹出窗口的代码
connect(ui->list, &QWidget::customContextMenuRequested, this, &UserPlaylistsSubWidget::popUp);
void UserPlaylistsSubWidget::popUp(const QPoint &pos)
{
mPopup.popup(ui->list->viewport()->mapToGlobal(pos));
}
弄清楚了。不知道QTableView有自己的动作列表,可以用setContextMenuPolicy(Qt::ActionsContextMenu)
在自己的弹出窗口中显示它。所以这是正确的解决方案,F5快捷方式按预期工作:
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);