QtQuick学习笔记之QML键盘Keys事件

来源:互联网 发布:java 值得看的源码 编辑:程序博客网 时间:2024/06/05 14:09

记录Qtquick核心编程学习笔记:

import QtQuick 2.5
import QtQuick.Controls 1.4
Rectangle{
    width:300;
    height:400;
    id:root;
    focus: true;
    Keys.enabled: true;
    Keys.forwardTo: [moveText, textCheck];
    Keys.onEscapePressed: {
        Qt.quit();
    }
    Text{
        id:moveText;
        x:20;
        y:30;
        width: 200;
        height: 30;
        color: "red";
        text:"Hello world";
        font:{bold: true; pixelSize:24;}
        Keys.enabled: true;
        Keys.onPressed: {
            switch(event.key)
            {
            case Qt.Key_Left:
                x -= 10;
                break;
            case Qt.Key_Right:
                x += 10;
                break;
            case Qt.Key_Up:
                y -= 10;
                break;
            case Qt.Key_Down:
                y += 10;
                break;
            default:
                return;
            }
            event.accepted = true;
        }
    }
    CheckBox {
        id:textCheck;
        anchors.left:  parent.left;
        anchors.leftMargin: 10;
        anchors.bottom: parent.bottom;
        anchors.bottomMargin: 10;
        text: "checkBox";
        z:1;
    }
}

Keys专门为Item处理按键事件的对象

详细请查看QT帮助

0 0