한국어
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 118890
160 QScopedPointer 소개 및 사용법 makersweb 2019.11.29 4434
159 QML 바인딩 끊김 진단 makersweb 2020.11.08 4460
158 ApplicationWindow 와 메뉴바(MenuBar)구성 file makersweb 2020.01.04 4523
157 Embedded Linux 에서 Qt 및 Graphics Stack file 운영자 2020.05.27 4523
156 Qt MQTT 에 대해서 file makersweb 2020.06.02 4541
155 단일 인스턴스 Qt 응용 프로그램(Single-instance Application) makersweb 2022.06.23 4623
154 QRhi 에 대해서 file makersweb 2023.12.29 4657
153 Qt 6에서 QList 변경사항 makersweb 2020.10.08 4673
152 QML과 코루틴(Coroutines) makersweb 2020.11.03 4690
151 Qt 5 코드를 Qt 6로 포팅하기 위해 도움이 되는 Clazy Framework file makersweb 2021.03.01 4713
150 ShaderEffect QML Type을 이용한 버튼 클릭 효과 file makersweb 2020.05.22 4776
149 안드로이드 가상장치 사용 file makersweb 2019.01.13 4833
148 하드디스크 드라이브 여유 공간 계산 file makersweb 2023.01.15 4862
147 tslib의 ts_calibrate를 응용해서 Qt로 터치보정기능 구현 file makersweb 2019.04.06 4883
146 표를 만들고 PDF문서로 출력하기 file makersweb 2018.09.30 4893
145 QML 전역 객체 (Global Object) file makersweb 2019.04.10 4898
144 Qt Bluetooth 관련 기능 확인 사항 makersweb 2018.07.10 4903
143 Qt 6의 비동기 API makersweb 2020.10.19 4915
142 QML에서 D-Bus 통신 file makersweb 2023.03.15 4924
141 Qt 6의 C++ 프로퍼티 바인딩 예제 makersweb 2021.11.01 4956