한국어
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 86175
48 z-order 를 컨트롤 하기위한 방법 makersweb 2015.05.13 6594
47 Qml 사용자 ScrollBar 구현 file makersweb 2015.07.24 6234
46 QML에서 undefined를 확인하는 방법 makersweb 2017.11.29 1489
45 Qml 및 C++개발시 유용한 팁 [3] makersweb 2018.04.06 6004
» Qml에서 키보드 입력 이벤트 핸들링 file makersweb 2018.08.09 3608
43 QML에서 동적으로 텍스트 다국어 처리 file makersweb 2018.11.04 4234
42 Qml에서 커튼효과 구현 예제 - Shader Effects file 운영자 2018.12.05 1116
41 ShaderEffect QML Type 을 이용한 그래픽효과 file makersweb 2018.12.09 2121
40 Qml과 C++로 구현하는 GUI어플리케이션 file makersweb 2018.12.25 14004
39 Qml 기본 컴포넌트 강좌 (1) file makersweb 2019.01.03 12121
38 Qml 기본 컴포넌트 강좌 (2) [2] file makersweb 2019.01.05 8654
37 Qml 기본 컴포넌트 강좌 (3) - 배치(positioning) 컴포넌트 file 운영자 2019.02.10 4890
36 Qml 기본 컴포넌트 강좌 (4) - 모델 리스팅(Listing) file 운영자 2019.02.23 5325
35 GPU가 없는 장치에서 Qt Quick을 사용 makersweb 2019.04.02 1858
34 tslib의 ts_calibrate를 응용해서 Qt로 터치보정기능 구현 file makersweb 2019.04.06 1370
33 QML 전역 객체 (Global Object) file makersweb 2019.04.10 1056
32 QML과 QtQuick 모듈 개념과 기본 타입들 makersweb 2019.04.26 13439
31 QML에서 멀티 스레드(multithreading) 프로그래밍 file makersweb 2019.05.25 2673
30 QtCreator Design으로 GUI만들기 (QML로 만드는 Hello World -2) [1] file makersweb 2019.05.26 14977
29 QML 강좌 - 동적 Listing (ListView) file makersweb 2019.06.01 10123