GreenSquare.qml
import QtQuickRectangle {width: 100height: 100color: 'green'border.color: Qt.lighter(color)
}
BlueSquare.qml
import QtQuickRectangle {color: 'blue'width: 50height: 50border.color: Qt.lighter(color)property alias text: label.text //将text开放出去Text {id: labeltext: ''color: 'white'anchors.centerIn: parent}
}
04.qml
import QtQuickWindow {width: 640height: 480visible: truetitle: qsTr("Layout")// (1)GreenSquare {BlueSquare {text: '(1)'anchors.fill: parentanchors.margins: 8 //内边距为8px,和上面的填充一起写才能生效}}// (2)GreenSquare {x: 110BlueSquare {text: '(2)'anchors.left: parent.leftanchors.margins: 8y: 8}}// (3)GreenSquare {x: 220BlueSquare {id: b1text: '(3)'anchors.horizontalCenter: parent.horizontalCenter //水平居中height: 25 //原尺寸的一半y: 10}BlueSquare {text: '(3-1)'anchors.top: b1.bottomanchors.horizontalCenter: parent.horizontalCenterheight: 25width: 75anchors.margins: 8}}// (4)GreenSquare {x: 330BlueSquare {text: '(04)'anchors.centerIn: parent}}// (5)GreenSquare {x: 440BlueSquare {text: '(5)'anchors.centerIn: parentanchors.horizontalCenterOffset: -12 //往左偏移12px}}
}