[QT] How to set shortcut keys for Menu or Action in the menu bar or toolbar in MainWindow

1. Two ways to set shortcut keys

1.1 Add & before a letter of the title property of the control, (Alt+the letter) as a shortcut key

explicit QMenu(const QString &text, QWidget *parent = Q_NULLPTR);

explicit QAction(const QString &text, QObject *parent = nullptr);

When setting text for QMenu or Action, add & before a certain letter in the text, and you will find that there is no & symbol in the displayed text, only the text will be displayed when the interface is displayed. This is because the function of adding & before a certain letter in the text is to set a shortcut key. When you press the Alt key, the letter after the & symbol in the corresponding text will be underlined. The method of using the shortcut key is Alt+&后的那个字母. For example: "&New", the shortcut key is Alt + N; "Save &As", the shortcut key is Alt + A.

QAction* newAct = new QAction(tr("&New"), this);  //加在N前的&表示一种快捷键的方式,先按住Alt键,再按n键就会触发这个Action。类似于Ctrl+N的快捷键
QAction* saveAsAct = new QAction(tr("Save &As..."), this);  //在A前加上&符号表示一种快捷键(Ctrl+a)的方式,加在哪个字母前就表示使用哪个字母作为快捷键

1.2 Use setShortcuts, (Ctrl + custom letter) as a shortcut key

QAction* newAct = new QAction(tr("New"), this);
newAct->setShortcuts(QKeySequence::New);
//当QKeySequence中没有相应的快捷键时,可以自定义,如下:
newAct->setShortcuts(QKeySequence("Ctrl+N"))

The usage methods of the two shortcut keys are different: one is to use Altand the other is to use Ctrl.

2. Set shortcut keys for the menu in the menu bar

2.1 Test

When setting the title of the menu, add an & symbol before the letter, for example:

menu = new QMenu(QStringLiteral("项目(&p)"));

When the Alt key is pressed, there will be an underline under the p, indicating that there is a shortcut key, and the shortcut key is P.

insert image description here

2.2 Code

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QAction>
#include <QMainWindow>
#include <QMenu>

namespace Ui {
    
    
    class MainWindow;
}

class MainWindow : public QMainWindow {
    
    
    Q_OBJECT

public:
    explicit MainWindow(QWidget* parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow* ui;

    QMenu* menu;
    QAction* actionNew;
    QAction* actionOpen;
    QAction* actionSave;
};

#endif  // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    
    
    ui->setupUi(this);

    menu = new QMenu(QStringLiteral("项目(&p)"));
    ui->menuBar->addMenu(menu);

    QList<QAction*> actionList;
    actionNew = new QAction(QStringLiteral("新建项目"));
    actionOpen = new QAction(QStringLiteral("打开项目"));
    actionSave = new QAction(QStringLiteral("保存项目"));
    actionList.append(actionNew);
    actionList.append(actionOpen);
    actionList.append(actionSave);
    menu->addActions(actionList);
}

MainWindow::~MainWindow() {
    
     delete ui; }

3. Set shortcut keys for Actions in the menu bar or toolbar

3.1 Test

There are two ways to set shortcut keys for Action, both & and setShortcuts can be used.
When using setShortcuts, shortcut keys will be displayed behind the action.
The first method: hold down Alt first, then press P, the displayed N will be underlined, and then press N to trigger a new project.
The second method: Press Ctrl+N directly to trigger a new project.
insert image description here

3.2 Code

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QAction>
#include <QDialog>
#include <QMainWindow>
#include <QMenu>

namespace Ui {
    
    
    class MainWindow;
}

class MainWindow : public QMainWindow {
    
    
    Q_OBJECT

public:
    explicit MainWindow(QWidget* parent = 0);
    ~MainWindow();

private slots:
    void slotActionNewTrigger();

private:
    Ui::MainWindow* ui;

    QWidget* widget;
    QMenu* menu;
    QAction* actionNew;
    QAction* actionOpen;
    QAction* actionSave;
};

#endif  // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    
    
    ui->setupUi(this);
    widget = centralWidget();

    menu = new QMenu(QStringLiteral("项目(&p)"));
    ui->menuBar->addMenu(menu);

    QList<QAction*> actionList;
    actionNew = new QAction(QStringLiteral("新建项目(&N)"), this);
    actionNew->setShortcut(QKeySequence::New);  //设置快捷键
    //actionNew->setShortcut(QKeySequence("Ctrl+N"));  //设置快捷键的第二种写法
    actionOpen = new QAction(QStringLiteral("打开项目(&O)"), this);
    actionOpen->setShortcut(QKeySequence::Open);
    //actionOpen->setShortcut(QKeySequence("Ctrl+O"));
    actionSave = new QAction(QStringLiteral("保存项目(&S)"), this);
    actionSave->setShortcut(QKeySequence::Save);
    //actionSave->setShortcut(QKeySequence("Ctrl+S"));
    actionList.append(actionNew);
    actionList.append(actionOpen);
    actionList.append(actionSave);
    menu->addActions(actionList);

    connect(actionNew, &QAction::triggered, this, &MainWindow::slotActionNewTrigger);
}

MainWindow::~MainWindow() {
    
     delete ui; }

void MainWindow::slotActionNewTrigger() {
    
    
    QDialog* dialog = new QDialog(this);
    dialog->resize(300, 200);
    dialog->setWindowTitle(tr("新建项目"));
    dialog->setVisible(true);
}

//打开项目和保存项目的槽没写,可以自己写

Guess you like

Origin blog.csdn.net/WL0616/article/details/130157894