【发布时间】:2015-05-01 02:25:38
【问题描述】:
有没有办法调整自定义元素的大小以适应其内容?我在 QML 上写了一些按钮示例,但是如果 main.qml 中未定义大小,则每次播放场景时我都需要使用文本长度调整大小。抱歉,但无法通过网络找到有关该内容的内容,只能反转有关如何将内容适合父母的问题。有来源:
BreezeButton.qml
import QtQuick 2.2
Item {
id: root
width: 172
height: 72
property string caption: "Button"
property string iconSource: null
signal clicked
Rectangle {
id: body
border {
width: 2
color: "#808e8e"
}
anchors{
fill: parent
}
gradient: Gradient {
id: bodyGradient
GradientStop { position: 0.4; color: "#4d4d4d" }
GradientStop { position: 0.9; color: "#31363b" }
}
MouseArea{
id: bodyMouseArea
z: bodyText.z + 1
anchors {
fill: parent
}
hoverEnabled: true
onEntered: {
body.border.color = "#3daee9"
}
onExited: {
body.border.color = "#7f8c8d"
}
onPressed: {
body.color = "#3daee9"
body.gradient = null
}
onReleased: {
body.color = "#4d4d4d"
body.gradient = bodyGradient
}
onClicked: {
root.clicked()
}
}
Text {
id: bodyText
anchors {
top: body.top
bottom: body.bottom
left: icon.right
}
width: body.width - icon.width
font.pointSize: 14
color: "#fcfcfc"
text: caption
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
Image {
id: icon
source: iconSource
anchors {
left: body.left
top: body.top
bottom: body.bottom
leftMargin: 5
topMargin: 5
bottomMargin: 5
}
height: root.height - 8
width: icon.height
sourceSize.width: icon.width
sourceSize.height: icon.height
}
}
}
例如,基本的 QML 按钮可以在每次播放场景时调整大小以适应其文本长度。
【问题讨论】:
标签: qml custom-controls qtquick2 qtgui