Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Shortcut of QAction in contextMenu has no response



  • alt text

    connect(ui->listWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenu(QPoint)));
    
    void MainWindow::customContextMenu(const QPoint &pos)
        QAction *action_rename;
        QList<QAction *> actions;
        ......
        action_rename = new QAction(this);
        action_rename->setText("重命名");
        action_rename->setShortcut(QKeySequence(Qt::Key_M));
        actions.append(action_rename);
        ......
        foreach(QAction *action, actions){
            action->setShortcutVisibleInContextMenu(true);
        }
    
        QAction *result_action = QMenu::exec(actions, ui->listWidget->mapToGlobal(pos));
        ......
        if (result_action == action_rename) {
            ......
        }
    }
    

Log in to reply