布局元素( Layout Items)
QML使⽤anchors( 锚) 对元素进⾏布局。 anchoring( 锚定) 是基础元素对
象的基本属性, 可以被所有的可视化QML元素使⽤。

⼀个元素有6条锚定线( top顶, bottom底, left左, right右, horizontalCenter⽔平中, verticalCenter垂直中) 。
每⼀条锚定线都有⼀个偏移( offset) 值, 在top( 顶) , bottom( 底) , left( 左) , right( 右) 的锚定线中它们也被称作边距。 对于horizontalCenter( ⽔平中) 与verticalCenter( 垂直中) 与baseline( ⽂本基线) 中被称作偏移值。
左对齐
BigSquare{
anchors.centerIn: parent
SmallSquare{
anchors.left: parent.left
}
}

右对齐
BigSquare{
anchors.centerIn: parent
SmallSquare{
anchors.right: parent.right
}
}

左下对齐
BigSquare{
anchors.centerIn: parent
SmallSquare{
anchors.left: parent.left
anchors.bottom: parent.bottom
}
}

右上对齐
BigSquare{
anchors.centerIn: parent
SmallSquare{
anchors.right: parent.right
anchors.top: parent.top
}
}

居中
BigSquare{
anchors.centerIn: parent
SmallSquare{
anchors.centerIn: parent
}
}

垂直对齐
BigSquare{
anchors.centerIn: parent
SmallSquare{
anchors.verticalCenter: parent.verticalCenter
}
}

水平对齐
BigSquare{
anchors.centerIn: parent
SmallSquare{
anchors.horizontalCenter: parent.horizontalCenter
}
}

左外对齐,加边距
BigSquare{
anchors.centerIn: parent
SmallSquare{
anchors.right: parent.left
anchors.rightMargin: 10
}
}

右下对齐,加边距
BigSquare{
anchors.centerIn: parent
SmallSquare{
anchors.right: parent.right
anchors.top: parent.bottom
anchors.topMargin: 10
}
}

情况不一一列举
输⼊元素( Input Element)
⽂本输⼊( TextInput) 单行
⽂本输⼊允许⽤户输⼊⼀⾏⽂本。 这个元素⽀持使⽤正则表达式验证器来限制输⼊和输⼊掩码的模式设置
Rectangle{
width: 200
height: 100
color: "linen"
TextInput{
id: input
x: 10; y: 10
width: 180
height: 80
//裁切-防止超出边界
clip: true
//焦点
focus: true
text: "Text Input"
}
}

⽂本编辑( TextEdit)多行
显示一块可编辑、格式化的文本
Rectangle{
width: 200
height: 100
color: "linen"
TextEdit{
id: input
x: 10; y: 10
width: 180
height: 80
//裁切-防止超出边界
clip: true
//焦点
focus: true
//设置换行模式
wrapMode: TextEdit.Wrap
text: "Text Edit"
}
}

按键元素( Key Element)
MySquare{
x: 20; y: 20
focus: true
Keys.onLeftPressed: x-=5
Keys.onRightPressed: x+=5
Keys.onUpPressed: y-=5
Keys.onDownPressed: y+=5
}
源代码

网友评论