我有一个 4x4 网格,我想将箭头键按下与网格内项目的移动相关联。如何做到这一点?
这是一个示例 QML:
import QtQuick 1.1
Rectangle {
id: main;
width: 500; height: 500;
color: "darkgreen";
property int emptyBlock: 16;
Grid {
id: grid16;
x: 5; y: 5;
width: 490; height: 490;
rows: 4; columns: 4; spacing: 5;
Repeater {
model: 1;
Rectangle {
width: 118; height: 118; color: "darkblue";
}
}
}
Keys.onRightPressed: pressRight();
function pressRight() {
console.log("Left key pressed");
}
focus: true;
}
更新 1:感谢 sebasgo 和 alexisdm 的回答。如果在网格内移动不是那么容易,为什么我们有move
过渡属性 [http://qt-project.org/doc/qt-4.8/qml-grid.html#move-prop]