한국어
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 118373
160 QML 바인딩 끊김 진단 makersweb 2020.11.08 4371
159 단일 인스턴스 Qt 응용 프로그램(Single-instance Application) makersweb 2022.06.23 4408
158 QScopedPointer 소개 및 사용법 makersweb 2019.11.29 4432
157 Qt MQTT 에 대해서 file makersweb 2020.06.02 4438
156 Embedded Linux 에서 Qt 및 Graphics Stack file 운영자 2020.05.27 4453
155 ApplicationWindow 와 메뉴바(MenuBar)구성 file makersweb 2020.01.04 4517
154 QRhi 에 대해서 file makersweb 2023.12.29 4551
153 QML과 코루틴(Coroutines) makersweb 2020.11.03 4566
152 Qt 6에서 QList 변경사항 makersweb 2020.10.08 4567
151 Qt로 작성된 안드로이드 APP에 Splash Screen을 추가 file makersweb 2020.03.10 4582
150 Qt 5 코드를 Qt 6로 포팅하기 위해 도움이 되는 Clazy Framework file makersweb 2021.03.01 4598
149 하드디스크 드라이브 여유 공간 계산 file makersweb 2023.01.15 4657
148 tslib의 ts_calibrate를 응용해서 Qt로 터치보정기능 구현 file makersweb 2019.04.06 4686
147 안드로이드 가상장치 사용 file makersweb 2019.01.13 4729
146 Qt Bluetooth 관련 기능 확인 사항 makersweb 2018.07.10 4732
145 ShaderEffect QML Type을 이용한 버튼 클릭 효과 file makersweb 2020.05.22 4776
144 Qt 6의 C++ 프로퍼티 바인딩 예제 makersweb 2021.11.01 4782
143 Qbs 프로젝트를 정의하기 위해 사용되는 몇가지 중요한 아이템들 file makersweb 2019.10.13 4783
142 Qt 스마트 포인터 (QSharedPointer, QScopedPointer, QPointer) makersweb 2022.08.18 4857
141 QML 전역 객체 (Global Object) file makersweb 2019.04.10 4883