【问题标题】:QMenu - shortcut is not triggeredQMenu - 快捷方式未触发
【发布时间】:2019-04-02 19:54:15
【问题描述】:

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

【问题讨论】:

  • 能否请您至少显示您提出mPopup菜单的代码?
  • 在上面添加代码

标签: qt qtableview qmenu qaction


【解决方案1】:

想通了。不知道 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);

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 2018-03-04
    • 1970-01-01
    相关资源
    最近更新 更多