한국어
Qt
 

Qml에서 키보드 입력 이벤트를 핸들링하는 방법이다. 아래 예제 소스코드를 보자!

 

main.cpp에서는 main.qml을 로드한다.

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

 

main.qml에서 키이벤트를 핸들링 하기위한 Item을 추가했으며 keys.onPressed 시그널에 대한 처리를 구현하였다.

import QtQuick 2.11
import QtQuick.Window 2.11

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Text {
        id: idText
        text: qsTr("text")
        anchors.centerIn: parent
        font.pixelSize: 15
    }

    Item {
        focus: true
        Keys.onPressed: {
            switch(event.key){
            case Qt.Key_A :
                idText.text = "input A key"
                break;
            case Qt.Key_0 :
                idText.text = "input num0 key"
                break;
            default:
                break;
            }
        }
    }
}

 

키보드의 "a" 키 또는 숫자 "0"키가 눌려지면 가운데 텍스트를 바꾼다.

input.png

 

 

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 88455
59 ShaderEffect QML Type을 이용한 버튼 클릭 효과 file makersweb 2020.05.22 1234
58 QML 전역 객체 (Global Object) file makersweb 2019.04.10 1207
57 QScopedPointer 소개 및 사용법 makersweb 2019.11.29 1186
56 Qt 6에서 QList 변경사항 makersweb 2020.10.08 1181
55 QML에서 D-Bus 통신 file makersweb 2023.03.15 1159
54 Qt MQTT 에 대해서 file makersweb 2020.06.02 1159
53 Qt로 XML 파싱 : Qt 6에서 업데이트된 (Parsing XML with Qt: Updates for Qt 6) [1] j2doll 2020.01.16 1124
52 Qt 6.0의 개발 호스트 및 대상 플랫폼 makersweb 2020.09.16 1122
51 [Qt] Google Play의 향후 요구 사항을 준수하는 방법 [2] j2doll 2019.07.29 1107
50 Qt기반의 서버와 클라이언트간 SOAP(Simple Object Access Protocol) file makersweb 2020.05.11 1098
49 QML 바인딩 끊김 진단 makersweb 2020.11.08 1094
48 Qt 5 코드를 Qt 6로 포팅하기 위해 도움이 되는 Clazy Framework file makersweb 2021.03.01 1086
47 Qt로 작성된 iOS 앱에서 시리얼 통신 file makersweb 2022.04.30 1081
46 Qt 5.15 및 Qt 6의 출시 일정 makersweb 2020.04.09 1070
45 Qt 응용프로그램에서 Lottie Animation사용 file makersweb 2021.05.30 1061
44 클라우드용 Qt file makersweb 2024.01.16 1033
43 OpacityMask 예제 file makersweb 2023.01.26 1028
42 QML 에서 QR코드 생성 file makersweb 2021.08.20 1026
41 Qt로 작성된 안드로이드 APP에 Splash Screen을 추가 file makersweb 2020.03.10 1025
40 QML에서 Websocket 서버와 통신 file makersweb 2021.09.18 1025