我有以下ColumnLayout:
ColumnLayout{
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
anchors.centerIn: parent
spacing: 0.065 * root.height
id: searchLayout
SearchTextRow {
text: qsTr("Search by title")
}
InputSearch {
id: titleText
}
RowLayout{
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
SubmitButton {
id: searchTitleButton
onClicked:{
biTitle.running = true;
recipeFetcher.searchByTitle(titleText.text);
}
}
BusyIndicator{
scale: Math.min(root.width * 0.002, root.height * 0.002)
id: biTitle
running: false
}
}
}
在 RowLayout 中,我想定位提交按钮和繁忙指示器,使提交按钮完全位于水平中间,而繁忙指示器位于其右侧。以下代码将把它们都视为一个元素并将该元素居中,这是我不想要的。
这是自定义 SubmitButton 类的代码:
RoundButton {
scale: Math.min(root.width * 0.002, root.height * 0.002)
Layout.alignment: Qt.AlignCenter
contentItem: Text {
text: "Submit"
color: hovered ? "darkred" : "black"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}
background: Rectangle {
opacity: 1
border.color: "orange"
radius: parent.radius
color: "orange"
layer.enabled: parent.hovered
layer.effect: DropShadow {
transparentBorder: true
color: "orange"
samples: 40
}
}
}
还有另一个问题,因为我想根据应用程序的分辨率缩放元素,以便它们与屏幕的大小成正比。
假设我对两个或其中一个元素进行缩放(
scale: Math.min(root.width * 0.002, root.height * 0.002)
),缩放后位置将被破坏:
在这张图片中,您可以看到,提交按钮和忙碌指示器居中,就像它们是同一个元素一样。
缩放 SubmitButton 和 BusyIndicator 后,出现此问题:
经过实验我找到了答案,我问缩放的原因也是因为解决方案本身也可以解决它,在本例中它确实解决了。与父母和孩子一起使用一点奇怪的诡计,这很有效:
SubmitButton {
id: searchIngredientsButton
onClicked:{
biSearch.running = true;
}
BusyIndicator{
anchors.left: parent.right
anchors.bottom: parent.bottom
anchors.top: parent.top
anchors.leftMargin: width/10
id: biSearch
running: false
}
}