한국어
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 85342
136 멀티 스레드환경, 스레드에 안전한 이벤트처리 makersweb 2016.10.27 4930
135 C++로 구현된 모델을 QML의 ListView에서 참조 file makersweb 2019.09.07 4883
134 Qml 기본 컴포넌트 강좌 (3) - 배치(positioning) 컴포넌트 file 운영자 2019.02.10 4823
133 Qt 멀티 스레드 프로그래밍 시 유의해야 할 몇 가지 makersweb 2020.01.13 4800
132 QNetworkAccessManager를 통해 HTTP POST 하는 예제 makersweb 2019.01.17 4715
131 Qt응용프로그램 실행 시 콘솔창(터미널)같이 띄우기 file makersweb 2019.01.16 4440
130 라즈베리파이4에 대한 Qt 5.14.1 크로스컴파일 [1] file makersweb 2020.02.12 4437
129 Windows에서 Qt D-Bus를 사용하여 프로세스간 통신(IPC) file makersweb 2019.05.02 4430
128 QML에서 동적으로 텍스트 다국어 처리 file makersweb 2018.11.04 4162
127 Qt기반의 오픈소스 프로젝트들 - 2 운영자 2019.07.21 3990
126 QPA 플러그인과 EGLFS file makersweb 2017.11.21 3898
125 열거형(enum)을 QML에서 사용하는 방법과 문자열(QString)로 얻기 makersweb 2019.08.20 3831
124 Qt 3D Studio 시작하기 file makersweb 2018.01.11 3803
123 Qt 를 사용하거나 기반으로 하는 응용프로그램 file makersweb 2021.01.30 3710
122 QML에서 앵커(anchors)로 위치 지정 file makersweb 2021.10.05 3700
121 Qt Logging Rule, Qt 프레임워크 로그 출력 makersweb 2017.01.13 3672
» Qml에서 키보드 입력 이벤트 핸들링 file makersweb 2018.08.09 3539
119 Qt Bluetooth를 이용한 시리얼(Serial) 통신 file makersweb 2019.02.17 3496
118 Qt Version확인 방법 makersweb 2018.03.29 3488
117 QML, 이미지, 폰트등을 바이너리 리소스로 만들기 makersweb 2019.06.24 3455