基本上我想顯示一個文本,告訴用戶開關是打開還是關閉。“開”和“關”這兩個詞的背景很小,所以我認為制作兩個標簽可能會奏效。現在我遇到了一個問題,我對第二個標簽的位置進行了硬編碼,它看起來不干凈,因為它們都沒有居中。這是代碼:
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.0
Window {
visible: true
width: 400
height: 400
property bool switchOn: false
Rectangle {
color: '#D3D3D3'
anchors.fill: parent
Label {
id: sentence
text: qsTr("The switch is currently turned ")
color: 'black'
width: parent.width
wrapMode: Text.Wrap
font.pixelSize: 40
anchors.top: parent.top
Label {
id: endText
text: switchOn ? qsTr(" off ") : qsTr(" on ")
font.pixelSize: 40
color: "white"
anchors {
top: parent.top
left: parent.left
topMargin: 50
leftMargin: 310
}
// @disable-check M16
background: Rectangle {
color: switchOn ? "grey" : "red"
radius: 8
}
}
}
Switch {
id: switch1
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 150
text: qsTr("Switch")
onToggled: switchOn = !switchOn
}
}
}
我怎樣才能讓那個句子作為一個句子出現并將它放在一個父物件中?
uj5u.com熱心網友回復:
您可以將標簽放在一行中,并將行水平居中。
Rectangle {
color: '#D3D3D3'
anchors.fill: parent
Row {
anchors.horizontalCenter: parent.horizontalCenter
Label {
id: sentence
...
}
Label {
id: endText
anchors.top: sentence.top
...
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/339929.html
