Item(基础元素对象),Rectangle(矩形框),Text(文本),Image(图像),MouseArea(鼠标区域)
Row,Column,Grid,Flow,anchors,TextInput(文本输入)和TextEdit(文本编辑)
State,transitions
// rectangle.qml
import QtQuick 2.0
// The root element is the Rectangle
Rectangle {
// name this element root
id: root
// properties: <name>: <value>
width: 120; height: 240
// color property
color: "#D8D8D8"
// Declare a nested element (child of root)
Image {
id: rocket
// reference the parent
x: (parent.width - width)/2; y: 40
source: 'assets/rocket.png'
}
// Another child of root
Text {
// un-named element
// reference element by id
y: rocket.y + rocket.height + 20
// reference root element
width: root.width
horizontalAlignment: Text.AlignHCenter
text: 'Rocket'
}
}
Text {
// (1) identifier
id: thisLabel
// (2) set x- and y-position
x: 24; y: 16
// (3) bind height to 2 * width
height: 2 * width
// (4) custom property
property int times: 24
// (5) property alias
property alias anotherTimes: thisLabel.times
// (6) set text appended by value
text: "Greetings " + times
// (7) font is a grouped property
font.family: "Ubuntu"
font.pixelSize: 24
// (8) KeyNavigation is an attached property
KeyNavigation.tab: otherLabel
// (9) signal handler for property changes
onHeightChanged: console.log('height:', height)
// focus is neeed to receive key events
focus: true
// change color based on focus value
color: focus?"red":"black"
}
Text {
id: label
x: 24; y: 24
// custom counter property for space presses
property int spacePresses: 0
text: "Space pressed: " + spacePresses + " times"
// (1) handler for text changes
onTextChanged: console.log("text changed to:", text)
// need focus to receive key events
focus: true
// (2) handler with some JS
Keys.onSpacePressed: {
increment()
}
// clear the text on escape
Keys.onEscapePressed: {
label.text = ''
}
// (3) a JS function
function increment() {
spacePresses = spacePresses + 1
}
}
//按键元素(Key Element)
// keys.qml
import QtQuick 2.0
DarkSquare {
width: 400; height: 200
GreenSquare {
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;
}
}
}
//动画
// animation.qml
import QtQuick 2.0
Image {
source: "assets/background.png"
Image {
x: 40; y: 80
source: "assets/rocket.png"
NumberAnimation on x {
to: 240
duration: 4000
loops: Animation.Infinite
}
RotationAnimation on rotation {
to: 360
duration: 4000
loops: Animation.Infinite
}
}
}