한국어
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 116946
160 QML 바인딩 끊김 진단 makersweb 2020.11.08 4039
159 QMake 프로젝트를 CMake 프로젝트로 변환 with qmake2cmake makersweb 2022.09.17 4069
158 Qt MQTT 에 대해서 file makersweb 2020.06.02 4074
157 단일 인스턴스 Qt 응용 프로그램(Single-instance Application) makersweb 2022.06.23 4121
156 QScopedPointer 소개 및 사용법 makersweb 2019.11.29 4181
155 QRhi 에 대해서 file makersweb 2023.12.29 4209
154 Qbs 프로젝트를 정의하기 위해 사용되는 몇가지 중요한 아이템들 file makersweb 2019.10.13 4219
153 Embedded Linux 에서 Qt 및 Graphics Stack file 운영자 2020.05.27 4223
152 Qt 5 코드를 Qt 6로 포팅하기 위해 도움이 되는 Clazy Framework file makersweb 2021.03.01 4228
151 QML과 코루틴(Coroutines) makersweb 2020.11.03 4242
150 Qt 6에서 QList 변경사항 makersweb 2020.10.08 4262
149 ApplicationWindow 와 메뉴바(MenuBar)구성 file makersweb 2020.01.04 4299
148 하드디스크 드라이브 여유 공간 계산 file makersweb 2023.01.15 4322
147 tslib의 ts_calibrate를 응용해서 Qt로 터치보정기능 구현 file makersweb 2019.04.06 4327
146 안드로이드 가상장치 사용 file makersweb 2019.01.13 4407
145 Qt Bluetooth 관련 기능 확인 사항 makersweb 2018.07.10 4419
144 Qt 6의 C++ 프로퍼티 바인딩 예제 makersweb 2021.11.01 4456
143 ShaderEffect QML Type을 이용한 버튼 클릭 효과 file makersweb 2020.05.22 4477
142 Qt 5.15 및 Qt 6의 출시 일정 makersweb 2020.04.09 4520
141 Qt 스마트 포인터 (QSharedPointer, QScopedPointer, QPointer) makersweb 2022.08.18 4522