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
        QtObject
        {
            id:usrVal;
            property int count;
            property int likeX:0;
            property int likeY:0;
        }
        Keys.onPressed:
        {
            console.log(event.key);
            switch(event.key)
            {
            case Qt.Key_Left:
                text1.color=Qt.rgba(Math.random(),Math.random(),Math.random());
                text1.x=text1.x-10;
                usrVal.likeX-=10;
                break;
            case Qt.Key_Right:
                 text1.color=Qt.rgba(Math.random(),Math.random(),Math.random());
                text1.x=text1.x+10;
                usrVal.likeX+=10;
                break;
            case Qt.Key_Down:
                 text1.color=Qt.rgba(Math.random(),Math.random(),Math.random());
                text1.y=text1.y+10;
                usrVal.likeY+=10;
                break;
            case Qt.Key_Up:
                 text1.color=Qt.rgba(Math.random(),Math.random(),Math.random());
                usrVal.likeY-=10;
                text1.y=text1.y-10;
                break;
            }
            event.acceptd=true;
        }
        Keys.enabled: true;
        Keys.forwardTo: [text1,like];
        Text
        {

            id: text1
            x: 105
            y: 96
            focus: true;
            width: 107
            height: 57

            color: "#f62424"
            text: qsTr("移动")
            antialiasing: false
            horizontalAlignment: Text.AlignLeft
            font.pixelSize: 150

        }

        CheckBox {
            id: like
            x: usrVal.likeX;
            y: usrVal.likeY;
            width: 138
            height: 14
            text: qsTr("like")
            checked: false
        }
        Component.onCompleted:
        {
            usrVal.count=0;
            text1.text=usrVal.count.toString();
            countDown.start();
        }

        Timer
        {
           id:countDown
           interval: 1000
           repeat: true
           running: false
           triggeredOnStart:  false
           onTriggered:
           {
               usrVal.count+=1;
               if(usrVal.count==10)
               {
                   text1.text="时间到了!";
                    text1.color=Qt.rgba(Math.random(),Math.random(),Math.random());
                   stop();
               }
               else
               {
                    text1.color=Qt.rgba(Math.random(),Math.random(),Math.random());
                    text1.text=usrVal.count.toString();
               }
           }

        }

    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值