当前位置: 代码迷 >> QT开发 >> QML的小程序,错哪了
  详细解决方案

QML的小程序,错哪了

热度:239   发布时间:2016-04-25 03:02:54.0
QML的小程序,哪里错了?

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow
{
    visible: true
    id:root
    width: 640
    height: 480
    Rectangle {
        id: rectangle1
        anchors.fill: parent
        focus: true;
        Text
        {
            Keys.enabled: true;
            Keys.forwardTo: [text1,like];
            id: text1
            x: 105
            y: 96
            width: 107
            height: 57
            color: "#f62424"
            text: qsTr("移动")
            antialiasing: false
            horizontalAlignment: Text.AlignLeft
            font.pixelSize: 50
            Keys.onPressed:
            {
                console.log(event.key);
                switch(event.key)
                {
                case Qt.Key_Left:
                    x=x-10;
                    break;
                case Qt.Key_Right:
                    x=x+10;
                    break;
                case Qt.Key_Down:
                    y=y+10;
                    break;
                case Qt.Key_Up:
                    y=y-10;
                    break;
                }
                event.acceptd=true;
            }
        }

        CheckBox {
            id: like
            x: 0
            y: 466
            width: 138
            height: 14
            text: qsTr("like")
        }


    }


}

------解决思路----------------------
Keys.onPressed提出来,Text里面接收不到键盘消息

Text
        {
            Keys.enabled: true;
            Keys.forwardTo: [text1,like];
            id: text1
            x: 105
            y: 96
            width: 107
            height: 57
            color: "#f62424"
            text: qsTr("移动")
            antialiasing: false
            horizontalAlignment: Text.AlignLeft
            font.pixelSize: 50

        }
        Keys.onPressed:
        {
            console.log(event.key);
            switch(event.key)
            {
            case Qt.Key_Left:
                text1.x=text1.x-10;
                break;
            case Qt.Key_Right:
                text1.x=text1.x+10;
                break;
            case Qt.Key_Down:
                text1.y=text1.y+10;
                break;
            case Qt.Key_Up:
                text1.y=text1.y-10;
                break;
            }
            event.acceptd=true;
        }
  相关解决方案