Qt开发记录10——功能开发——快捷键设置

实现效果

点击快捷键设置按钮,弹窗对话框
在这里插入图片描述
点击文本输入框,文本输入框获得焦点后,按下键盘按键,文本输入框获得按键值。即该快捷键设置为该按键。

在保存后,关闭对话框。此时快捷键设置内容保存到本地文本中,并对相应按钮的快捷键做出更新。

如此,使用按钮快捷键时,便能使用修改后的快捷键值了。

编码

自定义弹窗类ShortcutSetDialog

新建shortcutsetdialog.h文件,创建自定义实体类ShortcutSetDialog

#ifndef SHORTCUTSETDIALOG_H
#define SHORTCUTSETDIALOG_H

#include <QAbstractButton>
#include <QDialog>
#include <QKeyEvent>

namespace Ui {
class ShortcutSetDialog;
}

class ShortcutSetDialog : public QDialog
{
    Q_OBJECT

public:
    explicit ShortcutSetDialog(QWidget *parent = nullptr);
    ~ShortcutSetDialog();

    virtual void closeEvent(QCloseEvent *event);
    virtual void keyPressEvent(QKeyEvent *event);

    // 初始化
    void init();

signals:
    void initShortcut();

private slots:
    void on_buttonBox_clicked(QAbstractButton *button);

private:
    Ui::ShortcutSetDialog *ui;

    /* 提示信息对话框 */
    void showMessage(QString msg);
    /* 保存输入框数据到本地txt文本,并更新快捷键值 */
    bool save();

};

#endif // SHORTCUTSETDIALOG_H

新建shortcutsetdialog.cpp文件


#include <QPushButton>
#include <QDebug>
#include <QMessageBox>

#include "shortcutsetdialog.h"
#include "ui_shortcutsetdialog.h"
#include "src/utils/utils.h"

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

    Qt::WindowFlags flags = Qt::Dialog;flags |=Qt::WindowCloseButtonHint;
    setWindowFlags(flags);

    ui->buttonBox->button(QDialogButtonBox::Save)->setFlat(true);
    ui->buttonBox->button(QDialogButtonBox::Reset)->setFlat(true);
    ui->buttonBox->button(QDialogButtonBox::Close)->setFlat(true);

    ui->buttonBox->button(QDialogButtonBox::Save)->setText("保存");
    ui->buttonBox->button(QDialogButtonBox::Reset)->setText("重置");
    ui->buttonBox->button(QDialogButtonBox::Close)->setText("关闭");

    ui->buttonBox->button(QDialogButtonBox::Save)->setFixedWidth(60);
    ui->buttonBox->button(QDialogButtonBox::Save)->setFixedHeight(30);
    ui->buttonBox->button(QDialogButtonBox::Reset)->setFixedWidth(60);
    ui->buttonBox->button(QDialogButtonBox::Reset)->setFixedHeight(30);
    ui->buttonBox->button(QDialogButtonBox::Close)->setFixedWidth(60);
    ui->buttonBox->button(QDialogButtonBox::Close)->setFixedHeight(30);

    QString button_style = "QPushButton{background-color:blue; color: white; border-radius: 10px; border: 2px groove gray; border-style: outset;}""QPushButton:hover{background-color:white; color: blue;}""QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset;}";
    ui->buttonBox->button(QDialogButtonBox::Save)->setStyleSheet(button_style);
    ui->buttonBox->button(QDialogButtonBox::Reset)->setStyleSheet(button_style);
    ui->buttonBox->button(QDialogButtonBox::Close)->setStyleSheet(button_style);
}

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

/**
 * @brief 提示信息对话框
 * @param msg 提示信息
 */
void ShortcutSetDialog::showMessage(QString msg)
{
    QMessageBox::information(this, "信息", msg, QMessageBox::Ok,QMessageBox::NoButton);
}

/**
 * @brief 捕获关闭窗体事件
 * @param event 事件
 */
void ShortcutSetDialog::closeEvent(QCloseEvent *event)
{
    qDebug() << "ShortcutSetDialog::closeEvent 释放键盘事件";
    this->releaseKeyboard();
}

/**
 * @brief 捕获键盘按下事件
 * @param event 键盘按键事件
 */
void ShortcutSetDialog::keyPressEvent(QKeyEvent* event)
{
    qDebug() << "ShortcutSetDialog::keyPressEvent";
    // event->modifiers() 表示可以使用Ctrl键
    QKeySequence temp = QKeySequence(event->modifiers() + event->key());
    qDebug() << "捕获键盘按键:" << temp.toString();
    if(ui->lineEdit_scan->hasFocus()) {
        ui->lineEdit_scan->setText(temp.toString());
        qDebug() << "设置扫描:" << temp.toString();
        return;
    } else if(ui->lineEdit_previous_page->hasFocus()) {
        ui->lineEdit_previous_page->setText(temp.toString());
        qDebug() << "设置上一页:" << temp.toString();
        return;
    } else if(ui->lineEdit_next_page->hasFocus()) {
        ui->lineEdit_next_page->setText(temp.toString());
        qDebug() << "设置下一页:" << temp.toString();
        return;
    } else if(ui->lineEdit_return_init->hasFocus()) {
        ui->lineEdit_return_init->setText(temp.toString());
        qDebug() << "设置恢复到初始状态:" << temp.toString();
        return;
    } else if(ui->lineEdit_undo->hasFocus()) {
        ui->lineEdit_undo->setText(temp.toString());
        qDebug() << "设置撤销:" << temp.toString();
        return;
    } else if(ui->lineEdit_redo->hasFocus()) {
        ui->lineEdit_redo->setText(temp.toString());
        qDebug() << "设置恢复:" << temp.toString();
        return;
    } else if(ui->lineEdit_peripheral_erasure->hasFocus()) {
        ui->lineEdit_peripheral_erasure->setText(temp.toString());
        qDebug() << "设置周边擦除:" << temp.toString();
        return;
    } else if(ui->lineEdit_eraser->hasFocus()) {
        ui->lineEdit_eraser->setText(temp.toString());
        qDebug() << "设置橡皮檫:" << temp.toString();
        return;
    } else if(ui->lineEdit_reverse_erase->hasFocus()) {
        ui->lineEdit_reverse_erase->setText(temp.toString());
        qDebug() << "设置反向擦除:" << temp.toString();
        return;
    } else if(ui->lineEdit_right_1_degree->hasFocus()) {
        ui->lineEdit_right_1_degree->setText(temp.toString());
        qDebug() << "设置向右偏转1°:" << temp.toString();
        return;
    } else if(ui->lineEdit_left_1_degree->hasFocus()) {
        ui->lineEdit_left_1_degree->setText(temp.toString());
        qDebug() << "设置向左偏转1°:" << temp.toString();
        return;
    } else if(ui->lineEdit_lossless_right_1_degree->hasFocus()) {
        ui->lineEdit_lossless_right_1_degree->setText(temp.toString());
        qDebug() << "设置无损向右偏转1°:" << temp.toString();
        return;
    } else if(ui->lineEdit_lossless_left_1_degree->hasFocus()) {
        ui->lineEdit_lossless_left_1_degree->setText(temp.toString());
        qDebug() << "设置无损向左偏转1°:" << temp.toString();
        return;
    } else if(ui->lineEdit_flip_h->hasFocus()) {
        ui->lineEdit_flip_h->setText(temp.toString());
        qDebug() << "设置水平翻转:" << temp.toString();
        return;
    } else if(ui->lineEdit_flip_v->hasFocus()) {
        ui->lineEdit_flip_v->setText(temp.toString());
        qDebug() << "设置垂直翻转:" << temp.toString();
        return;
    } else if(ui->lineEdit_rotate_right_90_degrees->hasFocus()) {
        ui->lineEdit_rotate_right_90_degrees->setText(temp.toString());
        qDebug() << "设置向右旋转90°:" << temp.toString();
        return;
    } else if(ui->lineEdit_rotate_left_90_degrees->hasFocus()) {
        ui->lineEdit_rotate_left_90_degrees->setText(temp.toString());
        qDebug() << "设置向左旋转90°:" << temp.toString();
        return;
    } else if(ui->lineEdit_auto_correction->hasFocus()) {
        ui->lineEdit_auto_correction->setText(temp.toString());
        qDebug() << "设置自动纠偏:" << temp.toString();
        return;
    } else if(ui->lineEdit_screenshot->hasFocus()) {
        ui->lineEdit_screenshot->setText(temp.toString());
        qDebug() << "设置截图:" << temp.toString();
        return;
    } else if(ui->lineEdit_remove_noise->hasFocus()) {
        ui->lineEdit_remove_noise->setText(temp.toString());
        qDebug() << "设置去除噪点:" << temp.toString();
        return;
    } else if(ui->lineEdit_sharpen->hasFocus()) {
        ui->lineEdit_sharpen->setText(temp.toString());
        qDebug() << "设置文字锐化:" << temp.toString();
        return;
    } else if(ui->lineEdit_deepen->hasFocus()) {
        ui->lineEdit_deepen->setText(temp.toString());
        qDebug() << "设置文字加深:" << temp.toString();
        return;
    } else if(ui->lineEdit_lighten->hasFocus()) {
        ui->lineEdit_lighten->setText(temp.toString());
        qDebug() << "设置文字变浅:" << temp.toString();
        return;
    }

    QWidget::keyPressEvent(event);
}

void ShortcutSetDialog::init()
{
    // 扫描
    ui->lineEdit_scan->setText(getValueByKey("oneBtn"));

    // 上一页、下一页
    ui->lineEdit_previous_page->setText(getValueByKey("sevenBtn"));
    ui->lineEdit_next_page->setText(getValueByKey("eightBtn"));

    // 恢复到初始状态、撤销、恢复
    ui->lineEdit_return_init->setText(getValueByKey("twentyeightBtn"));
    ui->lineEdit_undo->setText(getValueByKey("thirtytwoBtn"));
    ui->lineEdit_redo->setText(getValueByKey("twentysevenBtn"));

    // 周边擦除、橡皮檫、反向擦除
    ui->lineEdit_peripheral_erasure->setText(getValueByKey("thirtyThreeBtn"));
    ui->lineEdit_eraser->setText(getValueByKey("twentyfourBtn"));
    ui->lineEdit_reverse_erase->setText(getValueByKey("twentynineBtn"));

    // 偏转
    ui->lineEdit_right_1_degree->setText(getValueByKey("seventeenBtn"));
    ui->lineEdit_left_1_degree->setText(getValueByKey("eighteenBtn"));
    ui->lineEdit_lossless_right_1_degree->setText(getValueByKey("thirtyfourBtn"));
    ui->lineEdit_lossless_left_1_degree->setText(getValueByKey("thirtyfiveBtn"));

    // 翻转
    ui->lineEdit_flip_h->setText(getValueByKey("nineteenBtn"));
    ui->lineEdit_flip_v->setText(getValueByKey("twentyBtn"));

    // 旋转90度
    ui->lineEdit_rotate_right_90_degrees->setText(getValueByKey("twelveBtn"));
    ui->lineEdit_rotate_left_90_degrees->setText(getValueByKey("elevenBtn"));

    // 其他
    ui->lineEdit_auto_correction->setText(getValueByKey("twentytwoBtn"));
    ui->lineEdit_screenshot->setText(getValueByKey("twentythreeBtn"));
    ui->lineEdit_remove_noise->setText(getValueByKey("twentysixBtn"));

    // 文字处理
    ui->lineEdit_sharpen->setText(getValueByKey("twentyfiveBtn"));
    ui->lineEdit_deepen->setText(getValueByKey("thirtyBtn"));
    ui->lineEdit_lighten->setText(getValueByKey("thirtyoneBtn"));
}

void ShortcutSetDialog::on_buttonBox_clicked(QAbstractButton *button)
{
    if(button == ui->buttonBox->button(QDialogButtonBox::Save)) {
        qDebug() << "保存按钮";

        if(save()) {
            this->close();
        }
    } else if (button == ui->buttonBox->button(QDialogButtonBox::Reset)) {
        qDebug() << "重置按钮";

        // 扫描
        ui->lineEdit_scan->setText("F1");

        // 上一页、下一页
        ui->lineEdit_previous_page->setText("Q");
        ui->lineEdit_next_page->setText("SPACE");

        // 恢复到初始状态、撤销、恢复
        ui->lineEdit_return_init->setText("A");
        ui->lineEdit_undo->setText("Ctrl+Z");
        ui->lineEdit_redo->setText("Ctrl+X");

        // 周边擦除、橡皮檫、反向擦除
        ui->lineEdit_peripheral_erasure->setText("Ctrl+F");
        ui->lineEdit_eraser->setText("F");
        ui->lineEdit_reverse_erase->setText("G");

        // 偏转
        ui->lineEdit_right_1_degree->setText("E");
        ui->lineEdit_left_1_degree->setText("W");
        ui->lineEdit_lossless_right_1_degree->setText("Ctrl+E");
        ui->lineEdit_lossless_left_1_degree->setText("Ctrl+W");

        // 翻转
        ui->lineEdit_flip_h->setText("O");
        ui->lineEdit_flip_v->setText("P");

        // 旋转90度
        ui->lineEdit_rotate_right_90_degrees->setText("R");
        ui->lineEdit_rotate_left_90_degrees->setText("T");

        // 其他
        ui->lineEdit_auto_correction->setText("D");
        ui->lineEdit_screenshot->setText("V");
        ui->lineEdit_remove_noise->setText("K");

        // 文字处理
        ui->lineEdit_sharpen->setText("U");
        ui->lineEdit_deepen->setText("Y");
        ui->lineEdit_lighten->setText("L");

        save();
    } else if (button == ui->buttonBox->button(QDialogButtonBox::Close)) {
        qDebug() << "关闭按钮";

        this->close();
    }

}

bool ShortcutSetDialog::save()
{
    QString msg;

    // 扫描
    if (ui->lineEdit_scan->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "扫描");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("oneBtn", ui->lineEdit_scan->text());
    }

    // 上一页、下一页
    if (ui->lineEdit_previous_page->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "上一页");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("sevenBtn", ui->lineEdit_previous_page->text());
    }
    if (ui->lineEdit_next_page->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "下一页");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("eightBtn", ui->lineEdit_next_page->text());
    }

    // 恢复到初始状态、撤销、恢复
    if (ui->lineEdit_return_init->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "恢复到初始状态");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twentyeightBtn", ui->lineEdit_return_init->text());
    }
    if (ui->lineEdit_undo->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "撤销");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("thirtytwoBtn", ui->lineEdit_undo->text());
    }
    if (ui->lineEdit_redo->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "恢复");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twentysevenBtn", ui->lineEdit_redo->text());
    }

    // 周边擦除、橡皮檫、反向擦除
    if (ui->lineEdit_peripheral_erasure->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "周边擦除");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("thirtyThreeBtn", ui->lineEdit_peripheral_erasure->text());
    }
    if (ui->lineEdit_eraser->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "橡皮擦");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twentyfourBtn", ui->lineEdit_eraser->text());
    }
    if (ui->lineEdit_reverse_erase->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "反向擦除");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twentynineBtn", ui->lineEdit_reverse_erase->text());
    }

    // 偏转
    if (ui->lineEdit_right_1_degree->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "向右偏转1°");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("seventeenBtn", ui->lineEdit_right_1_degree->text());
    }
    if (ui->lineEdit_left_1_degree->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "向左偏转1°");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("eighteenBtn", ui->lineEdit_left_1_degree->text());
    }
    if (ui->lineEdit_lossless_right_1_degree->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "无损向右偏转1°");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("thirtyfourBtn", ui->lineEdit_lossless_right_1_degree->text());
    }
    if (ui->lineEdit_lossless_left_1_degree->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "无损向左偏转1°");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("thirtyfiveBtn", ui->lineEdit_lossless_left_1_degree->text());
    }

    // 翻转
    if (ui->lineEdit_flip_h->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "水平翻转");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("nineteenBtn", ui->lineEdit_flip_h->text());
    }
    if (ui->lineEdit_flip_v->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "垂直翻转");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twentyBtn", ui->lineEdit_flip_v->text());
    }

    // 旋转90度
    if (ui->lineEdit_rotate_right_90_degrees->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "向右旋转90°");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twelveBtn", ui->lineEdit_rotate_right_90_degrees->text());
    }
    if (ui->lineEdit_rotate_left_90_degrees->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "向左旋转90°");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("elevenBtn", ui->lineEdit_rotate_left_90_degrees->text());
    }

    // 其他
    if (ui->lineEdit_auto_correction->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "自动纠偏");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twentytwoBtn", ui->lineEdit_auto_correction->text());
    }
    if (ui->lineEdit_screenshot->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "截图");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twentythreeBtn", ui->lineEdit_screenshot->text());
    }
    if (ui->lineEdit_remove_noise->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "去除噪点");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twentysixBtn", ui->lineEdit_remove_noise->text());
    }

    // 文字处理
    if (ui->lineEdit_sharpen->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "文字锐化");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("twentyfiveBtn", ui->lineEdit_sharpen->text());
    }
    if (ui->lineEdit_deepen->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "文字加深");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("thirtyBtn", ui->lineEdit_deepen->text());
    }
    if (ui->lineEdit_lighten->text() == nullptr) {
        msg.sprintf("请输入【%s】快捷键!", "文字变浅");
        showMessage(msg);
        return false;
    } else {
        setValueByKey("thirtyoneBtn", ui->lineEdit_lighten->text());
    }

    // 更新快捷键值
    emit initShortcut();
    return true;
}

其中,有工具类utils.cpp

/**
 * @brief 判断快捷键文件是否存在
 * @return true或者false
 */
bool checkShortcutExit()
{
    QString path = g_cadrePath + "\\shortcut.txt";
    if (isFileExit(path)) {
        return true;
    }
    return false;
}
/**
 * @brief 初始化快捷键文件
 * @return true或者false
 */
bool initShortcutTxt()
{
    QString path = g_cadrePath + "\\shortcut.txt";
    QSettings config(path, QSettings::IniFormat);

    config.setValue("oneBtn", "F1");
    config.setValue("twoBtn", "F2");
    config.setValue("threeBtn", "F3");
    config.setValue("fourBtn", "F4");
    config.setValue("fiveBtn", "F5");
    config.setValue("sixBtn", "DELETE");
    config.setValue("sevenBtn", "Q");
    config.setValue("eightBtn", "SPACE");
    config.setValue("nineBtn", "M");
    config.setValue("tenBtn", "N");
    config.setValue("elevenBtn", "T");
    config.setValue("twelveBtn", "R");
    config.setValue("thirteenBtn", "B");
    config.setValue("fourteenBtn", "C");
    config.setValue("fifteenBtn", "I");
    config.setValue("sixteenBtn", "S");
    config.setValue("seventeenBtn", "E");
    config.setValue("eighteenBtn", "W");
    config.setValue("nineteenBtn", "O");
    config.setValue("twentyBtn", "P");
    config.setValue("twentyoneBtn", "H");
    config.setValue("twentytwoBtn", "D");
    config.setValue("twentythreeBtn", "V");
    config.setValue("twentyfourBtn", "F");
    config.setValue("twentyfiveBtn", "U");
    config.setValue("twentysixBtn", "K");
    config.setValue("twentysevenBtn", "Ctrl+X");
    config.setValue("twentyeightBtn", "A");
    config.setValue("twentynineBtn", "G");
    config.setValue("thirtyBtn", "Y");
    config.setValue("thirtyoneBtn", "L");
    config.setValue("thirtytwoBtn", "Ctrl+Z");
    config.setValue("thirtyThreeBtn", "Ctrl+F");
    config.setValue("thirtyfourBtn", "Ctrl+E");
    config.setValue("thirtyfiveBtn", "Ctrl+W");
    config.setValue("thirtysixBtn", "Ctrl+S");

    return true;
}
/**
 * @brief 根据键获取值
 * @param key 快捷键键值对的键
 * @return 快捷键键值对的值
 */
QString getValueByKey(QString key) {
    QString path = g_cadrePath + "\\shortcut.txt";
    QSettings config(path, QSettings::IniFormat);
    return config.value(key).toString();
}

/**
 * @brief 设置快捷键
 * @param key 快捷键键值对的键
 * @param value 快捷键键值对的值
 * @return true or false
 */
bool setValueByKey(QString key, QString value) {
    try {
        QString path = g_cadrePath + "\\shortcut.txt";
        QSettings config(path, QSettings::IniFormat);
        config.setValue(key, value);
        return true;

    } catch (Exception) {
        std::cout << "设置快捷键异常\n";
    }
    return false;
}

新建shortcutsetdialog.ui文件,创建ui界面

在这里插入图片描述
shortcutsetdialog.ui 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ShortcutSetDialog</class>
 <widget class="QDialog" name="ShortcutSetDialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>470</width>
    <height>500</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>快捷键设置</string>
  </property>
  <property name="toolTip">
   <string/>
  </property>
  <property name="statusTip">
   <string/>
  </property>
  <property name="whatsThis">
   <string/>
  </property>
  <property name="modal">
   <bool>false</bool>
  </property>
  <widget class="QDialogButtonBox" name="buttonBox">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>450</y>
     <width>211</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Close|QDialogButtonBox::Reset|QDialogButtonBox::Save</set>
   </property>
   <property name="centerButtons">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>190</x>
     <y>10</y>
     <width>71</width>
     <height>16</height>
    </rect>
   </property>
   <property name="text">
    <string>快捷键设置</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_scan">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>40</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>F1</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_scan">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>40</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>扫描:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_reverse_erase">
   <property name="geometry">
    <rect>
     <x>410</x>
     <y>160</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>G</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_reverse_erase">
   <property name="geometry">
    <rect>
     <x>350</x>
     <y>160</y>
     <width>60</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>反向擦除:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_rotate_right_90_degrees">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>320</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>R</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_rotate_right_90_degrees">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>320</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>向右旋转90°:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_previous_page">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>80</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>Q</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_previous_page">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>80</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>上一页:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_next_page">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>80</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>SPACE</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_next_page">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>80</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>下一页:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_return_init">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>120</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>A</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_return_init">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>120</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>恢复到初始状态:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_undo">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>120</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>Ctrl+Z</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_undo">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>120</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>撤销:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_redo">
   <property name="geometry">
    <rect>
     <x>410</x>
     <y>120</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>Ctrl+X</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_redo">
   <property name="geometry">
    <rect>
     <x>350</x>
     <y>120</y>
     <width>60</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>恢复:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_eraser">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>160</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>橡皮擦:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_peripheral_erasure">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>160</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>周边擦除:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_eraser">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>160</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>F</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_peripheral_erasure">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>160</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>Ctrl+F</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_lossless_right_1_degree">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>240</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>无损向右偏转1°:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_flip_h">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>280</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>水平翻转:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_lossless_left_1_degree">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>240</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>无损向左偏转1°:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_flip_h">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>280</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>O</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_lossless_right_1_degree">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>240</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>Ctrl+E</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_rotate_left_90_degrees">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>320</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>向左旋转90°:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_left_1_degree">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>200</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>向左偏转1°:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_flip_v">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>280</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>P</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_flip_v">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>280</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>垂直翻转:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_lossless_left_1_degree">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>240</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>Ctrl+W</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_left_1_degree">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>200</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>W</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_right_1_degree">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>200</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>E</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_rotate_left_90_degrees">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>320</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>T</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_right_1_degree">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>200</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>向右偏转1°:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_remove_noise">
   <property name="geometry">
    <rect>
     <x>350</x>
     <y>360</y>
     <width>60</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>去除噪点:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_deepen">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>400</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>文字加深:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_sharpen">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>400</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>文字锐化:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_deepen">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>400</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>Y</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_remove_noise">
   <property name="geometry">
    <rect>
     <x>410</x>
     <y>360</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>K</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_screenshot">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>360</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>截图:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_lighten">
   <property name="geometry">
    <rect>
     <x>410</x>
     <y>400</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>L</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_lighten">
   <property name="geometry">
    <rect>
     <x>350</x>
     <y>400</y>
     <width>60</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>文字变浅:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_sharpen">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>400</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>U</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_screenshot">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>360</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>V</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_auto_correction">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>360</y>
     <width>50</width>
     <height>30</height>
    </rect>
   </property>
   <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="inputMethodHints">
    <set>Qt::ImhFormattedNumbersOnly</set>
   </property>
   <property name="text">
    <string>D</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="label_auto_correction">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>360</y>
     <width>100</width>
     <height>30</height>
    </rect>
   </property>
   <property name="text">
    <string>自动纠偏:</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

打开窗体时,捕获键盘事件grabKeyboard()

在mainwindow.cpp构造时,new一个ShortcutSetDialog

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

    connect(m_topToolBar, &TopToolBar::openShortcutSetDialog, this, &MainWindow::onOpenShortcutSetDialog);
    dialog = new ShortcutSetDialog(this);
    connect(dialog, &ShortcutSetDialog::initShortcut, this, &MainWindow::onInitShortcut);
	// 快捷键
    shortcut();
}

void MainWindow::shortcut()
{
    qDebug() << "MainWindow::shortcut...........";

    // 初始化快捷键
    if(!checkShortcutExit()) {
        initShortcutTxt();
    }
    temp1 = new QShortcut(m_toolBar->get_pushButton_initial());
    temp2 = new QShortcut(m_toolBar->get_pushButton_undo());
    temp3 = new QShortcut(m_toolBar->get_pushButton_redo());

    temp4 = new QShortcut(m_toolBar->get_pushButton_clean_side());
    temp5 = new QShortcut(m_toolBar->get_pushButton_clean());
    temp6 = new QShortcut(m_toolBar->get_pushButton_clean_reverse());

    temp7 = new QShortcut(m_toolBar->get_pushButton_rotate_l_2());
    temp8 = new QShortcut(m_toolBar->get_pushButton_rotate_l_3());
    temp9 = new QShortcut(m_toolBar->get_pushButton_rotate_r_3());
    temp10 = new QShortcut(m_toolBar->get_pushButton_rotate_r_2());

    temp11 = new QShortcut(m_toolBar->get_pushButton_mirror_h());
    temp12 = new QShortcut(m_toolBar->get_pushButton_mirror_v());
    temp13 = new QShortcut(m_toolBar->get_pushButton_rotate_l());
    temp14 = new QShortcut(m_toolBar->get_pushButton_rotate_r());

    temp15 = new QShortcut(m_toolBar->get_pushButton_auto_correct());
    temp16 = new QShortcut(m_toolBar->get_pushButton_capture());
    temp17 = new QShortcut(m_toolBar->get_pushButton_reduce_noise());

    temp18 = new QShortcut(m_toolBar->get_pushButton_text_sharp());
    temp19 = new QShortcut(m_toolBar->get_pushButton_text_deepen());
    temp20 = new QShortcut(m_toolBar->get_pushButton_text_shallow());

    temp21 = new QShortcut(this);
    temp22 = new QShortcut(this);
    temp23 = new QShortcut(this);
    temp24 = new QShortcut(m_topToolBar->get_pushButton_scan_single());
    // 设置快捷键的值
    onInitShortcut();
    // 连接信号与槽函数
    temp1->setAutoRepeat(false);
    connect(temp1, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_initial_clicked()));
    temp2->setAutoRepeat(false);
    connect(temp2, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_undo_clicked()));
    temp3->setAutoRepeat(false);
    connect(temp3, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_redo_clicked()));
    temp4->setAutoRepeat(false);
    connect(temp4, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_clean_side_clicked()));
    temp5->setAutoRepeat(false);
    connect(temp5, SIGNAL(activated()), m_toolBar, SLOT(shortcut_pushButton_clean()));
    temp6->setAutoRepeat(false);
    connect(temp6, SIGNAL(activated()), m_toolBar, SLOT(shortcut_pushButton_clean_reverse()));
    temp7->setAutoRepeat(false);
    connect(temp7, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_rotate_l_2_clicked()));
    temp8->setAutoRepeat(false);
    connect(temp8, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_rotate_l_3_clicked()));
    temp9->setAutoRepeat(false);
    connect(temp9, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_rotate_r_3_clicked()));
    temp10->setAutoRepeat(false);
    connect(temp10, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_rotate_r_2_clicked()));
    temp11->setAutoRepeat(false);
    connect(temp11, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_mirror_h_clicked()));
    temp12->setAutoRepeat(false);
    connect(temp12, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_mirror_v_clicked()));
    temp13->setAutoRepeat(false);
    connect(temp13, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_rotate_l_clicked()));
    temp14->setAutoRepeat(false);
    connect(temp14, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_rotate_r_clicked()));
    temp15->setAutoRepeat(false);
    connect(temp15, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_auto_correct_clicked()));
    temp16->setAutoRepeat(false);
    connect(temp16, SIGNAL(activated()), m_toolBar, SLOT(shortcut_pushButton_capture()));
    temp17->setAutoRepeat(false);
    connect(temp17, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_reduce_noise_clicked()));
    temp18->setAutoRepeat(false);
    connect(temp18, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_text_sharp_clicked()));
    temp19->setAutoRepeat(false);
    connect(temp19, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_text_deepen_clicked()));
    temp20->setAutoRepeat(false);
    connect(temp20, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_text_shallow_clicked()));
    temp21->setAutoRepeat(false);
    connect(temp21, SIGNAL(activated()), this, SLOT(previousPage()));
    temp22->setAutoRepeat(false);
    connect(temp22, SIGNAL(activated()), this, SLOT(nextPage()));
    temp23->setAutoRepeat(false);
    connect(temp23, SIGNAL(activated()), m_toolBar, SLOT(on_pushButton_3_clicked()));
    temp24->setAutoRepeat(false);
    connect(temp24, SIGNAL(activated()), m_topToolBar, SLOT(on_pushButton_scan_single_clicked()));
}

void MainWindow::onInitShortcut()
{
    // 快捷键-恢复到初始状态
    temp1->setKey(QKeySequence(getValueByKey("twentyeightBtn")));
    QString text1 = static_cast<QPushButton*>(temp1->parent())->text();
    static_cast<QPushButton*>(temp1->parent())->setText(text1.left(text1.lastIndexOf("(")) + "(" + getValueByKey("twentyeightBtn") + ")");
    qDebug() << "建立【恢复到初始状态】快捷键 :" << static_cast<QPushButton*>(temp1->parent())->text();

    // 快捷键-撤销
    temp2->setKey(QKeySequence(getValueByKey("thirtytwoBtn")));
    QString text2 = static_cast<QPushButton*>(temp2->parent())->text();
    static_cast<QPushButton*>(temp2->parent())->setText(text2.left(text2.lastIndexOf("(")) + "(" + getValueByKey("thirtytwoBtn") + ")");
    qDebug() << "建立【撤销】快捷键 :" << static_cast<QPushButton*>(temp2->parent())->text();

    // 快捷键-恢复
    temp3->setKey(QKeySequence(getValueByKey("twentysevenBtn")));
    QString text3 = static_cast<QPushButton*>(temp3->parent())->text();
    static_cast<QPushButton*>(temp3->parent())->setText(text3.left(text3.lastIndexOf("(")) + "(" + getValueByKey("twentysevenBtn") + ")");
    qDebug() << "建立【恢复】快捷键 :" << static_cast<QPushButton*>(temp3->parent())->text();

    // 快捷键-周边擦除
    temp4->setKey(QKeySequence(getValueByKey("thirtyThreeBtn")));
    QString text4 = static_cast<QPushButton*>(temp4->parent())->text();
    static_cast<QPushButton*>(temp4->parent())->setText(text4.left(text4.lastIndexOf("(")) + "(" + getValueByKey("thirtyThreeBtn") + ")");
    qDebug() << "建立【周边擦除】快捷键 :" << static_cast<QPushButton*>(temp4->parent())->text();

    // 快捷键-橡皮擦
    temp5->setKey(QKeySequence(getValueByKey("twentyfourBtn")));
    QString text5 = static_cast<QPushButton*>(temp5->parent())->text();
    static_cast<QPushButton*>(temp5->parent())->setText(text5.left(text5.lastIndexOf("(")) + "(" + getValueByKey("twentyfourBtn") + ")");
    qDebug() << "建立【橡皮擦】快捷键 :" << static_cast<QPushButton*>(temp5->parent())->text();

    // 快捷键-反向擦除
    temp6->setKey(QKeySequence(getValueByKey("twentynineBtn")));
    QString text6 = static_cast<QPushButton*>(temp6->parent())->text();
    static_cast<QPushButton*>(temp6->parent())->setText(text6.left(text6.lastIndexOf("(")) + "(" + getValueByKey("twentynineBtn") + ")");
    qDebug() << "建立【反向擦除】快捷键 :" << static_cast<QPushButton*>(temp6->parent())->text();

    // 快捷键-向右偏转1°
    temp7->setKey(QKeySequence(getValueByKey("seventeenBtn")));
    QString text7 = static_cast<QPushButton*>(temp7->parent())->text();
    static_cast<QPushButton*>(temp7->parent())->setText(text7.left(text7.lastIndexOf("(")) + "(" + getValueByKey("seventeenBtn") + ")");
    qDebug() << "建立【向右偏转1°】快捷键 :" << static_cast<QPushButton*>(temp7->parent())->text();

    // 快捷键-向左偏转1°
    temp8->setKey(QKeySequence(getValueByKey("eighteenBtn")));
    QString text8 = static_cast<QPushButton*>(temp8->parent())->text();
    static_cast<QPushButton*>(temp8->parent())->setText(text8.left(text8.lastIndexOf("(")) + "(" + getValueByKey("eighteenBtn") + ")");
    qDebug() << "建立【向左偏转1°】快捷键 :" << static_cast<QPushButton*>(temp8->parent())->text();

    // 快捷键-无损向右偏转1°
    temp9->setKey(QKeySequence(getValueByKey("thirtyfourBtn")));
    QString text9 = static_cast<QPushButton*>(temp9->parent())->text();
    static_cast<QPushButton*>(temp9->parent())->setText(text9.left(text9.lastIndexOf("(")) + "(" + getValueByKey("thirtyfourBtn") + ")");
    qDebug() << "建立【无损向右偏转1°】快捷键 :" << static_cast<QPushButton*>(temp9->parent())->text();

    // 快捷键-无损向左偏转1°
    temp10->setKey(QKeySequence(getValueByKey("thirtyfiveBtn")));
    QString text10 = static_cast<QPushButton*>(temp10->parent())->text();
    static_cast<QPushButton*>(temp10->parent())->setText(text10.left(text10.lastIndexOf("(")) + "(" + getValueByKey("thirtyfiveBtn") + ")");
    qDebug() << "建立【无损向左偏转1°】快捷键 :" << static_cast<QPushButton*>(temp10->parent())->text();

    // 快捷键-水平翻转
    temp11->setKey(QKeySequence(getValueByKey("nineteenBtn")));
    QString text11 = static_cast<QPushButton*>(temp11->parent())->text();
    static_cast<QPushButton*>(temp11->parent())->setText(text11.left(text11.lastIndexOf("(")) + "(" + getValueByKey("nineteenBtn") + ")");
    qDebug() << "建立【水平翻转】快捷键 :" << static_cast<QPushButton*>(temp11->parent())->text();

    // 快捷键-垂直翻转
    temp12->setKey(QKeySequence(getValueByKey("twentyBtn")));
    QString text12 = static_cast<QPushButton*>(temp12->parent())->text();
    static_cast<QPushButton*>(temp12->parent())->setText(text12.left(text12.lastIndexOf("(")) + "(" + getValueByKey("twentyBtn") + ")");
    qDebug() << "建立【垂直翻转】快捷键 :" << static_cast<QPushButton*>(temp12->parent())->text();

    // 快捷键-向左旋转90度
    temp13->setKey(QKeySequence(getValueByKey("elevenBtn")));
    QString text13 = static_cast<QPushButton*>(temp13->parent())->text();
    static_cast<QPushButton*>(temp13->parent())->setText(text13.left(text13.lastIndexOf("(")) + "(" + getValueByKey("elevenBtn") + ")");
    qDebug() << "建立【向左旋转90度】快捷键 :" << static_cast<QPushButton*>(temp13->parent())->text();

    // 快捷键-向右旋转90度
    temp14->setKey(QKeySequence(getValueByKey("twelveBtn")));
    QString text14 = static_cast<QPushButton*>(temp14->parent())->text();
    static_cast<QPushButton*>(temp14->parent())->setText(text14.left(text14.lastIndexOf("(")) + "(" + getValueByKey("twelveBtn") + ")");
    qDebug() << "建立【向右旋转90度】快捷键 :" << static_cast<QPushButton*>(temp14->parent())->text();

    // 快捷键-自动纠偏
    temp15->setKey(QKeySequence(getValueByKey("twentytwoBtn")));
    QString text15 = static_cast<QPushButton*>(temp15->parent())->text();
    static_cast<QPushButton*>(temp15->parent())->setText(text15.left(text15.lastIndexOf("(")) + "(" + getValueByKey("twentytwoBtn") + ")");
    qDebug() << "建立【自动纠偏】快捷键 :" << static_cast<QPushButton*>(temp15->parent())->text();

    // 快捷键-截图
    temp16->setKey(QKeySequence(getValueByKey("twentythreeBtn")));
    QString text16 = static_cast<QPushButton*>(temp16->parent())->text();
    static_cast<QPushButton*>(temp16->parent())->setText(text16.left(text16.lastIndexOf("(")) + "(" + getValueByKey("twentythreeBtn") + ")");
    qDebug() << "建立【截图】快捷键 :" << static_cast<QPushButton*>(temp16->parent())->text();

    // 快捷键-去除噪点
    temp17->setKey(QKeySequence(getValueByKey("twentysixBtn")));
    QString text17 = static_cast<QPushButton*>(temp17->parent())->text();
    static_cast<QPushButton*>(temp17->parent())->setText(text17.left(text17.lastIndexOf("(")) + "(" + getValueByKey("twentysixBtn") + ")");
    qDebug() << "建立【去除噪点】快捷键 :" << static_cast<QPushButton*>(temp17->parent())->text();

    // 快捷键-文字锐化
    temp18->setKey(QKeySequence(getValueByKey("twentyfiveBtn")));
    QString text18 = static_cast<QPushButton*>(temp18->parent())->text();
    static_cast<QPushButton*>(temp18->parent())->setText(text18.left(text18.lastIndexOf("(")) + "(" + getValueByKey("twentyfiveBtn") + ")");
    qDebug() << "建立【文字锐化】快捷键 :" << static_cast<QPushButton*>(temp18->parent())->text();

    // 快捷键-文字加深
    temp19->setKey(QKeySequence(getValueByKey("thirtyBtn")));
    QString text19 = static_cast<QPushButton*>(temp19->parent())->text();
    static_cast<QPushButton*>(temp19->parent())->setText(text19.left(text19.lastIndexOf("(")) + "(" + getValueByKey("thirtyBtn") + ")");
    qDebug() << "建立【文字加深】快捷键 :" << static_cast<QPushButton*>(temp19->parent())->text();

    // 快捷键-文字变浅
    temp20->setKey(QKeySequence(getValueByKey("thirtyoneBtn")));
    QString text20 = static_cast<QPushButton*>(temp20->parent())->text();
    static_cast<QPushButton*>(temp20->parent())->setText(text20.left(text20.lastIndexOf("(")) + "(" + getValueByKey("thirtyoneBtn") + ")");
    qDebug() << "建立【文字变浅】快捷键 :" << static_cast<QPushButton*>(temp20->parent())->text();

    // 快捷键-上一页
    temp21->setKey(QKeySequence(getValueByKey("sevenBtn")));
    qDebug() << "建立【上一页】快捷键 :" << temp21->key();

    // 快捷键-下一页
    temp22->setKey(QKeySequence(getValueByKey("eightBtn")));
    qDebug() << "建立【下一页】快捷键 :" << temp22->key();

    // 快捷键-保存
    temp23->setKey(QKeySequence(getValueByKey("thirtysixBtn")));
    qDebug() << "建立【保存】快捷键 :" << temp23->key();

    // 快捷键-扫描
    temp24->setKey(QKeySequence(getValueByKey("oneBtn")));
    QString text24 = static_cast<QPushButton*>(temp24->parent())->text();
    static_cast<QPushButton*>(temp24->parent())->setText(text24.left(text24.lastIndexOf("(")) + "(" + getValueByKey("oneBtn") + ")");
    qDebug() << "建立【扫描F1】快捷键 :" << static_cast<QPushButton*>(temp24->parent())->text();
}

在m_topToolBar面板中,打开快捷键弹窗时,发送信号

emit openShortcutSetDialog();

对应槽函数执行打开弹窗

void MainWindow::onOpenShortcutSetDialog()
{
    // 捕获键盘事件
    dialog->grabKeyboard();
    // 初始化快捷键值(从本地文本中获取)
    dialog->init();
    // 弹出窗口
    dialog->show();
}

捕获键盘按下事件keyPressEvent

在shortcutsetdialog.cpp文件中,keyPressEvent会执行捕获键盘按下事件,将按键值输入文本框中。需要注意的是,必须有上述的==dialog->grabKeyboard();==才行。

/**
 * @brief 捕获键盘按下事件
 * @param event 键盘按键事件
 */
void ShortcutSetDialog::keyPressEvent(QKeyEvent* event)
{
    qDebug() << "ShortcutSetDialog::keyPressEvent";
    // event->modifiers() 表示可以使用Ctrl键
    QKeySequence temp = QKeySequence(event->modifiers() + event->key());
    qDebug() << "捕获键盘按键:" << temp.toString();
    if(ui->lineEdit_scan->hasFocus()) {
        ui->lineEdit_scan->setText(temp.toString());
        qDebug() << "设置扫描:" << temp.toString();
        return;
    } else if(ui->lineEdit_previous_page->hasFocus()) {
        ui->lineEdit_previous_page->setText(temp.toString());
        qDebug() << "设置上一页:" << temp.toString();
        return;
    } else if(ui->lineEdit_next_page->hasFocus()) {
        ui->lineEdit_next_page->setText(temp.toString());
        qDebug() << "设置下一页:" << temp.toString();
        return;
    } else if(ui->lineEdit_return_init->hasFocus()) {
        ui->lineEdit_return_init->setText(temp.toString());
        qDebug() << "设置恢复到初始状态:" << temp.toString();
        return;
    } else if(ui->lineEdit_undo->hasFocus()) {
        ui->lineEdit_undo->setText(temp.toString());
        qDebug() << "设置撤销:" << temp.toString();
        return;
    } else if(ui->lineEdit_redo->hasFocus()) {
        ui->lineEdit_redo->setText(temp.toString());
        qDebug() << "设置恢复:" << temp.toString();
        return;
    } else if(ui->lineEdit_peripheral_erasure->hasFocus()) {
        ui->lineEdit_peripheral_erasure->setText(temp.toString());
        qDebug() << "设置周边擦除:" << temp.toString();
        return;
    } else if(ui->lineEdit_eraser->hasFocus()) {
        ui->lineEdit_eraser->setText(temp.toString());
        qDebug() << "设置橡皮檫:" << temp.toString();
        return;
    } else if(ui->lineEdit_reverse_erase->hasFocus()) {
        ui->lineEdit_reverse_erase->setText(temp.toString());
        qDebug() << "设置反向擦除:" << temp.toString();
        return;
    } else if(ui->lineEdit_right_1_degree->hasFocus()) {
        ui->lineEdit_right_1_degree->setText(temp.toString());
        qDebug() << "设置向右偏转1°:" << temp.toString();
        return;
    } else if(ui->lineEdit_left_1_degree->hasFocus()) {
        ui->lineEdit_left_1_degree->setText(temp.toString());
        qDebug() << "设置向左偏转1°:" << temp.toString();
        return;
    } else if(ui->lineEdit_lossless_right_1_degree->hasFocus()) {
        ui->lineEdit_lossless_right_1_degree->setText(temp.toString());
        qDebug() << "设置无损向右偏转1°:" << temp.toString();
        return;
    } else if(ui->lineEdit_lossless_left_1_degree->hasFocus()) {
        ui->lineEdit_lossless_left_1_degree->setText(temp.toString());
        qDebug() << "设置无损向左偏转1°:" << temp.toString();
        return;
    } else if(ui->lineEdit_flip_h->hasFocus()) {
        ui->lineEdit_flip_h->setText(temp.toString());
        qDebug() << "设置水平翻转:" << temp.toString();
        return;
    } else if(ui->lineEdit_flip_v->hasFocus()) {
        ui->lineEdit_flip_v->setText(temp.toString());
        qDebug() << "设置垂直翻转:" << temp.toString();
        return;
    } else if(ui->lineEdit_rotate_right_90_degrees->hasFocus()) {
        ui->lineEdit_rotate_right_90_degrees->setText(temp.toString());
        qDebug() << "设置向右旋转90°:" << temp.toString();
        return;
    } else if(ui->lineEdit_rotate_left_90_degrees->hasFocus()) {
        ui->lineEdit_rotate_left_90_degrees->setText(temp.toString());
        qDebug() << "设置向左旋转90°:" << temp.toString();
        return;
    } else if(ui->lineEdit_auto_correction->hasFocus()) {
        ui->lineEdit_auto_correction->setText(temp.toString());
        qDebug() << "设置自动纠偏:" << temp.toString();
        return;
    } else if(ui->lineEdit_screenshot->hasFocus()) {
        ui->lineEdit_screenshot->setText(temp.toString());
        qDebug() << "设置截图:" << temp.toString();
        return;
    } else if(ui->lineEdit_remove_noise->hasFocus()) {
        ui->lineEdit_remove_noise->setText(temp.toString());
        qDebug() << "设置去除噪点:" << temp.toString();
        return;
    } else if(ui->lineEdit_sharpen->hasFocus()) {
        ui->lineEdit_sharpen->setText(temp.toString());
        qDebug() << "设置文字锐化:" << temp.toString();
        return;
    } else if(ui->lineEdit_deepen->hasFocus()) {
        ui->lineEdit_deepen->setText(temp.toString());
        qDebug() << "设置文字加深:" << temp.toString();
        return;
    } else if(ui->lineEdit_lighten->hasFocus()) {
        ui->lineEdit_lighten->setText(temp.toString());
        qDebug() << "设置文字变浅:" << temp.toString();
        return;
    }

    QWidget::keyPressEvent(event);
}

关闭窗体时,释放键盘事件releaseKeyboard()

在点击窗口右上角×,或者点击了关闭、取消等按钮关闭窗口时,执行释放键盘事件函数。

/**
 * @brief 捕获关闭窗体事件
 * @param event 事件
 */
void ShortcutSetDialog::closeEvent(QCloseEvent *event)
{
    qDebug() << "ShortcutSetDialog::closeEvent 释放键盘事件";
    this->releaseKeyboard();
}

踩坑记录:多个窗口时,grabKeyboard()导致其他窗口获取不到键盘事件的问题

最开始,是直接将ShortcutSetDialog在m_topToolBar面板中new出来,并在new的时候grabKeyboard();,此时的父子级关系为mainwindow > m_topToolBar > ShortcutSetDialog

后来在弹窗其他窗体时,无法输入

QInputDialog *dialog = new QInputDialog(this, Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint);
int m_pageNum = dialog->getInt(this, "页码设置", "请输入设置的页码", inPageNo, 0, 1000, 1, &isOK, Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint);

在这里插入图片描述

所以,修改成了以上代码。主要修改有3点:
1、修改父子级关系为mainwindow > ShortcutSetDialog
2、不在new自定义类ShortcutSetDialog时grabKeyboard(),而是打开窗口时执行grabKeyboard()
3、关闭ShortcutSetDialog时,以ShortcutSetDialog中的closeEvent执行releaseKeyboard()

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,关于QT绘图系统,我可以为您提供一些基本的介绍和使用方法。 QT提供了一套强大的绘图系统,可以用来绘制各种图形、图表、文本、图像等等。QT的绘图系统主要由以下几个类组成: 1. QPainter:绘图类,用于绘制各种图形、图像、文本等。 2. QPen:画笔类,用于设置绘图的线条样式、颜色、粗细等。 3. QBrush:画刷类,用于设置绘图的填充样式、颜色等。 4. QFont:字体类,用于设置绘图的字体、大小、样式等。 5. QRect和QRectF:矩形类,用于表示矩形区域的位置和大小。 6. QImage:图像类,用于表示位图图像。 使用QT绘图系统进行绘图主要有两种方式,一种是在QWidget或QGraphicsView等控件的paintEvent事件中进行绘制,另一种是创建QPixmap或QImage等图像对象,然后使用QPainter在图像上进行绘制。 下面是一个简单的示例代码,演示了如何在QWidget中使用QT绘图系统进行绘制: ```cpp void MyWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); // 设置画笔和画刷 QPen pen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); QBrush brush(Qt::yellow, Qt::SolidPattern); painter.setPen(pen); painter.setBrush(brush); // 绘制矩形和椭圆 QRect rect(100, 100, 200, 100); painter.drawRect(rect); painter.drawEllipse(rect); // 绘制文本 QFont font("Arial", 20); painter.setFont(font); painter.drawText(rect, Qt::AlignCenter, "Hello, QT!"); } ``` 在这个示例代码中,我们在QWidget的paintEvent事件中创建了一个QPainter对象,然后设置了画笔和画刷,接着使用QRect定义了一个矩形区域,并使用drawRect和drawEllipse方法在该区域上绘制了矩形和椭圆。最后,使用setFont和drawText方法在矩形区域中央绘制了一段文本。 当然,QT绘图系统的功能远不止于此,您可以根据需要使用更多的API进行绘制。希望这个简单的介绍可以帮助您更好地了解和使用QT的绘图系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小言W

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值