2014年6月10日 星期二

在QML使用Keys原件 ( Keys Element In QML )

介紹:
  • 如何使用QML的Keys原件
Introduction:
  • How to used Keys element in QML 

開發環境:
  • QT 5.2
  • QtQuick 2.1
<Square.qml>

import QtQuick 2.0

Rectangle {
    id:root
    property alias color: root.color
    width : 60
    height : 60
    color : "#ea7025"
    border.color: Qt.lighter(color)
}

<main.qml>

Rectangle {
    id      : root
    width   : 300
    height  : 300
    color   : "#D8D8D8"

    Square{
        id: square
        x: 8; y: 8
    }
    focus: true
    Keys.onLeftPressed  : square.x -= 8
    Keys.onRightPressed : square.x += 8
    Keys.onUpPressed    : square.y -= 8
    Keys.onDownPressed  : square.y += 8
 //按上下左右鍵時,產生的對應動作
    Keys.onPressed:
    {
        switch(event.key) {
            case Qt.Key_Plus:
            square.scale += 0.2
            break;
            case Qt.Key_Minus:
            square.scale -= 0.2
            break;
        }
    }
    Keys.onDeletePressed: console.log("Delete Pressed")
    Keys.onDigit1Pressed: console.log("Digit1 Pressed")
    //onAsteriskPressed()-'*'星號
    //onBackPressed()-Backspace
    //onBacktabPressed()-Shift+Tab
    //onDeletePressed()-Delete
    //onDigit1Pressed()-數字鍵1
    //onEnterPressed()-Enter
    //onDownPressed()-Down
    //onEscapePressed()-Esc
}

0 意見:

張貼留言