Skip to content

Commit b366856

Browse files
committed
Add QmlKey
1 parent ece3e48 commit b366856

File tree

9 files changed

+152
-0
lines changed

9 files changed

+152
-0
lines changed

QmlKey/QmlKey.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*!
2+
*@file QmlKey.cpp
3+
*@brief Qml全局按键
4+
*@version 1.0
5+
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6+
*@author zhengtianzuo
7+
*/
8+
#include "QmlKey.h"
9+
10+
QmlKey::QmlKey()
11+
{
12+
13+
}
14+
15+
bool QmlKey::eventFilter(QObject *watched, QEvent *event)
16+
{
17+
if (event->type() == QEvent::KeyPress)
18+
{
19+
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
20+
if (keyEvent->key() == Qt::Key_Backspace)
21+
{
22+
emit sKeyBackPress();
23+
return(true);
24+
}
25+
}
26+
if (event->type() == QEvent::KeyRelease)
27+
{
28+
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
29+
if (keyEvent->key() == Qt::Key_Backspace)
30+
{
31+
emit sKeyBackRelease();
32+
return(true);
33+
}
34+
}
35+
36+
return QObject::eventFilter(watched, event);
37+
}

QmlKey/QmlKey.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*!
2+
*@file QmlKey.h
3+
*@brief Qml全局按键
4+
*@version 1.0
5+
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6+
*@author zhengtianzuo
7+
*/
8+
#pragma once
9+
#include <QObject>
10+
#include <QEvent>
11+
#include <QKeyEvent>
12+
13+
class QmlKey : public QObject
14+
{
15+
Q_OBJECT
16+
public:
17+
QmlKey();
18+
19+
protected:
20+
virtual bool eventFilter(QObject *watched, QEvent *event);
21+
22+
signals:
23+
void sKeyBackPress();
24+
void sKeyBackRelease();
25+
};

QmlKey/QmlKey.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#-------------------------------------------------
2+
#
3+
# Copyright (C) 2003-2103 CamelSoft Corporation
4+
#
5+
#-------------------------------------------------
6+
7+
QT += qml quick
8+
9+
CONFIG += c++11
10+
11+
SOURCES += main.cpp \
12+
QmlKey.cpp
13+
14+
HEADERS += \
15+
QmlKey.h
16+
17+
RESOURCES += qml.qrc

QmlKey/main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*!
2+
*@file main.cpp
3+
*@brief 程序主文件
4+
*@version 1.0
5+
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6+
*@author zhengtianzuo
7+
*/
8+
#include <QGuiApplication>
9+
#include <QQmlApplicationEngine>
10+
#include <QQmlContext>
11+
#include <QQuickWindow>
12+
#include "QmlKey.h"
13+
14+
int main(int argc, char *argv[])
15+
{
16+
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
17+
QGuiApplication app(argc, argv);
18+
QmlKey qmlKey;
19+
QQmlApplicationEngine engine;
20+
engine.rootContext()->setContextProperty("qmlKey", &qmlKey);
21+
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
22+
QObject *root = engine.rootObjects()[0];
23+
root->installEventFilter(&qmlKey);
24+
QObject::connect(&qmlKey, SIGNAL(sKeyBackPress()), root, SLOT(onSKeyBackPress()));
25+
QObject::connect(&qmlKey, SIGNAL(sKeyBackRelease()), root, SLOT(onSKeyBackRelease()));
26+
return app.exec();
27+
}

QmlKey/main.qml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*!
2+
*@file main.qml
3+
*@brief 主文件
4+
*@version 1.0
5+
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6+
*@author zhengtianzuo
7+
*/
8+
import QtQuick 2.7
9+
import QtQuick.Controls 2.0
10+
import QtQuick.Layouts 1.0
11+
import QtQuick.Window 2.2
12+
13+
Window {
14+
id: frmWindow
15+
visible: true
16+
width: 400
17+
height: 300
18+
title: qsTr("Qml全局按键")
19+
20+
Label{
21+
id: label
22+
text: qsTr("文本")
23+
height: 48
24+
width: 120
25+
anchors.centerIn: parent
26+
}
27+
28+
function onSKeyBackPress(){
29+
label.text = qsTr("按下了Backspace")
30+
}
31+
32+
function onSKeyBackRelease(){
33+
label.text = qsTr("松开了Backspace")
34+
}
35+
}

QmlKey/qml.qrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>main.qml</file>
4+
</qresource>
5+
</RCC>

QmlKey/show.gif

42.3 KB
Loading

QtQuickExamples.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ SUBDIRS += QmlProgress
3434
SUBDIRS += QmlWinExtras
3535
SUBDIRS += QmlCalendar
3636
SUBDIRS += QmlCanvasText
37+
SUBDIRS += QmlKey

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,8 @@ QmlCanvasText: QmlCanvas文字
152152

153153
![](https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlCanvasText/show.jpg?raw=true)
154154

155+
156+
QmlKey: Qml全局按键
157+
158+
![](https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlKey/show.gif?raw=true)
159+

0 commit comments

Comments
 (0)