<pre name="code" class="cpp"><span style="font-size:14px;">#include "mainwindow.h"
#include <QKeyEvent>
#include <QMessageBox>
#include <QDebug>
#include <QShortcut>
#include <QPushButton>
#include <QHBoxLayout>
#include <QAction>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
// 设置窗口布局
QWidget *mainWidget = new QWidget(this);
QPushButton *button1 = new QPushButton("MessageBoxA", this);
QPushButton *button2 = new QPushButton("MessageBoxB", this);
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(button1);
layout->addWidget(button2);
mainWidget->setLayout(layout);
this->setCentralWidget(mainWidget);
this->resize(200, 100);
this->setWindowTitle("Test");
// 创建QAction并设置快捷键
QAction *actionA = new QAction(this);
actionA->setShortcut(QKeySequence(tr("Ctrl+A")));
// actionA->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_A));
QAction *actionB = new QAction(this);
actionB->setShortcut(QKeySequence(tr("Ctrl+B")));
// actionA->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));
// 给button添加快捷键
// this->addAction(actionA);// 没用
button1->addAction(actionA);// 必须将快捷键添加到按钮上,同时使keyPressEvent失效,无法捕捉到Ctrl+A
button2->addAction(actionB);
// 设置快捷键相应
connect(actionA, &QAction::triggered, this, &MainWindow::button1_click);
connect(actionB, &QAction::triggered, this, &MainWindow::button2_click);
// 设置按钮点击事件相应
connect(button1, &QPushButton::clicked, this, &MainWindow::button1_click);
connect(button2, &QPushButton::clicked, this, &MainWindow::button2_click);
// 占用快捷键,使keyPressEvent函数失效
QShortcut *shortCut1 = new QShortcut(QKeySequence(tr("Delete")), this);
QShortcut *shortCut2 = new QShortcut(QKeySequence(tr("Return")), this);
// QShortcut *shortCut2 = new QShortcut(tr("Return"), this);
}
MainWindow::~MainWindow()
{
}
// 如果快捷键被设置,keyPressevent就无法处理相应的按钮事件
void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_0:
{
qDebug() << Qt::Key_0;
QMessageBox::about(NULL, "0", "0");
break;
}
case Qt::Key_Return:// 被占用,失效
{
qDebug() << Qt::Key_Return;
QMessageBox::about(NULL, "return", "return");
break;
}
case Qt::Key_Delete:// 被占用,失效
{
qDebug() << Qt::Key_Delete;
QMessageBox::about(NULL, "Delete", "Delete");
break;
}
case QKeySequence::Delete:// 捕获不到,不能有这种方式处理
{
qDebug() << QKeySequence::Delete;
QMessageBox::about(NULL, "QKeySequence::Delete", "QKeySequence::Delete");
break;
}
case Qt::Key_F1:
{
qDebug() << Qt::Key_F1;
QMessageBox::about(NULL, "F1", "F1");
break;
}
case Qt::Key_O:// 处理组合快捷按钮
{
if (event->modifiers() & Qt::ControlModifier)
{
QMessageBox::about(NULL, "Ctr+O", "Ctr+O");
}
break;
}
case Qt::Key_A:// 被窗口button占用,失效,不能处理
{
if (event->modifiers() & Qt::ControlModifier)
{
QMessageBox::about(NULL, "Ctr+A", "Ctr+A");
}
break;
}
default:
{
qDebug() << event->key();
}
}
// if (event->key() == Qt::Key_O)
// {
// if (event->modifiers() & Qt::ControlModifier)
// {
// QMessageBox::about(NULL, "Ctr+O", "Ctr+O");
// }
// }
}
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
}
void MainWindow::button1_click()
{
QMessageBox::about(NULL, "MessageBoxA", "MessageBoxA");
}
void MainWindow::button2_click()
{
QMessageBox::about(NULL, "MessageBoxB", "MessageBoxB");
}
</span>