代码如下:
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();
}
}
}
}
}