Qt 全局热键之使用QxtGlobalShortcut实现

本文介绍了如何在Qt5中使用QxtGlobalShortcut库实现全局热键功能,包括检测快捷键是否被占用、设置快捷键并连接事件处理。通过实例展示了自定义控件CustomKeySequenceEdit的使用和项目集成方法。
摘要由CSDN通过智能技术生成

Qt 全局热键之使用QxtGlobalShortcut实现

全局热键,我们普遍关心的三个问题:

  1. 检测快捷键是否被占用
  2. 注册(反注册)快捷键
  3. 快捷键被触发

本文就介绍QxtGlobalShortcut在Qt5中的应用,看看最简单使用步骤和例子:

1、把qxtglobalshortcut5文件放在项目目录下,在项目.pro加入一句,include(qxtglobalshortcut5-master/qxt.pri)(这个具体的目录名称,自己可以修改)
2、使用时包含头文件#include “qxtglobalshortcut.h”

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "qxtglobalshortcut.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QxtGlobalShortcut *shortcut = new QxtGlobalShortcut(this);
    if(shortcut->setShortcut(QKeySequence("Shift+1")))
    {
        connect(shortcut, &QxtGlobalShortcut::activated,
            [=]() {qDebug() << "shortcut activated";});
    }
    else
    {
        qDebug()<<"快捷键已占用";
    }
}

说明:shortcut->setShortcut(QKeySequence(“Shift+1”));
如果你输入的是QString(“”)这样不存在的快捷键,返回值也是false,和快捷键被占用返回的false值一样。
但是这样已经够我们使用了,只有你在这里的参数放入的是一个合法的快捷键或者快捷键组合的字符串(而非空字符串),那么就能检测出该快捷键是否被占用。

现在做一个在项目中可能会用到的例子:
先看运行结果:
在这里插入图片描述
我们可以捕获键盘输入的快捷键,然后按设置的快捷键就会触发,触发的次数会在label中显示出来。
第一步下载:https://github.com/ddqd/qxtglobalshortcut5
下载后解压出来放到我们新建的工程目录中,我们的qt工程是基于QWidget的,这里选择是这个(你可以选择其他的)
在这里插入图片描述
在这里插入图片描述 将下载后的解压出来,命名为qxtglobalshortcut5,进入这文件夹可以看到
在这里插入图片描述
第二步,将这个qxt.pri添加到我们的工厂pro文件中
在pro文件中添加include(./qxtglobalshortcut5/qxt.pri)
第三步,开始使用。
QKeySequenceEdit一次可以输入4组快捷键,中间用,分隔开的,这是Qt提供给我们的,在帮助文档中可以看到。
我们需要一个每次只能输入一个或者一组快捷键的编辑框。
customKeySequenceEdit.h

#ifndef CUSTOMKEYSEQUENCEEDIT_H
#define CUSTOMKEYSEQUENCEEDIT_H

#include <QKeySequenceEdit>

class CustomKeySequenceEdit : public QKeySequenceEdit
{
    Q_OBJECT

public:
    explicit CustomKeySequenceEdit(QWidget *parent = nullptr);

protected:
    void keyPressEvent(QKeyEvent *event) override;
};

#endif // CUSTOMKEYSEQUENCEEDIT_H

customKeySequenceEdit.cpp

#include "customKeySequenceEdit.h"
#include <QDebug>
#include <QKeyEvent>

CustomKeySequenceEdit::CustomKeySequenceEdit(QWidget *parent)
    : QKeySequenceEdit(parent)
{
//    this->setStyleSheet("QLineEdit{qproperty-alignment:AlignHCenter;}"); //设置文字居中
}

void CustomKeySequenceEdit::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Backspace)
    {
        this->clear();
    }
    else
    {
        QKeySequenceEdit::keyPressEvent(event);
        QString strKeySequence = keySequence().toString().split(",").first();
        QKeySequence seq(QKeySequence::fromString(strKeySequence));
        setKeySequence(seq);
    }

    emit editingFinished();
}

在这里插入图片描述 这个是提升为我们自定义的CustomKeySequenceEdit控件
widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include "qxtglobalshortcut.h"

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
    QxtGlobalShortcut *m_hotkey;
    int m_activedNum;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "customKeySequenceEdit.h"

#include <QDebug>
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , m_hotkey(new QxtGlobalShortcut(this))
    , m_activedNum(0)
{
    ui->setupUi(this);
    ui->label->clear();

    connect(ui->keySequenceEdit, &CustomKeySequenceEdit::editingFinished, this, [&](){
         m_activedNum = 0;
         ui->label->clear();

        if (m_hotkey->setShortcut(ui->keySequenceEdit->keySequence()))
        {
              qDebug() << "hot key is register sucess!";
        }
        else
        {
            if (!ui->keySequenceEdit->keySequence().isEmpty())
                qDebug() << "hot key is register failed!";
        }
    });

    connect(this->m_hotkey, &QxtGlobalShortcut::activated, [&](){
        ++m_activedNum;
        QString str = QString("shutcuts %1 actived %2 times").arg(ui->keySequenceEdit->keySequence().toString()).arg(m_activedNum);
        ui->label->setText(str);
    });
}

Widget::~Widget()
{
    delete ui;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值