新增全局热键示例

This commit is contained in:
feiyangqingyun
2021-10-13 14:16:13 +08:00
parent 64240782cd
commit 508876dd18
39 changed files with 4192 additions and 59 deletions

38
shortcut/frmshortcut.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include "frmshortcut.h"
#include "ui_frmshortcut.h"
#include "qxtglobalshortcut.h"
#include "qdatetime.h"
#include "qdebug.h"
frmShortCut::frmShortCut(QWidget *parent) : QWidget(parent), ui(new Ui::frmShortCut)
{
ui->setupUi(this);
this->initForm();
}
frmShortCut::~frmShortCut()
{
delete ui;
}
void frmShortCut::initForm()
{
//实例化热键类
QxtGlobalShortcut *shortcut = new QxtGlobalShortcut(QKeySequence("ctrl+x"), this);
connect(shortcut, SIGNAL(activated()), this, SLOT(shortcut()));
}
void frmShortCut::shortcut()
{
#if 1
//如果是最小化则显示,否则最小化
if (this->isMinimized()) {
this->showNormal();
this->activateWindow();
} else {
this->showMinimized();
}
#else
ui->label->setText("activated " + QTime::currentTime().toString("hh:mm:ss zzz"));
#endif
}

26
shortcut/frmshortcut.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef FRMSHORTCUT_H
#define FRMSHORTCUT_H
#include <QWidget>
namespace Ui {
class frmShortCut;
}
class frmShortCut : public QWidget
{
Q_OBJECT
public:
explicit frmShortCut(QWidget *parent = 0);
~frmShortCut();
private:
Ui::frmShortCut *ui;
private slots:
void initForm();
void shortcut();
};
#endif // FRMSHORTCUT_H

31
shortcut/frmshortcut.ui Normal file
View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmShortCut</class>
<widget class="QWidget" name="frmShortCut">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>全局热键示例</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>按 ctrl+x 最小化,再次按显示</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

10
shortcut/main.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include "frmshortcut.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
frmShortCut w;
w.show();
return a.exec();
}

16
shortcut/shortcut.pro Normal file
View File

@@ -0,0 +1,16 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
TARGET = shortcut
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += frmshortcut.cpp
HEADERS += frmshortcut.h
FORMS += frmshortcut.ui
INCLUDEPATH += $$PWD/../3rd_qxtglobalshortcut
include($$PWD/../3rd_qxtglobalshortcut/3rd_qxtglobalshortcut.pri)