如何防止qml矩形边框与内容重叠?
我想要一个矩形来自动调整大小,使其完全适合其视觉子对象。如果没有边界,那么以下方法非常有效:如何防止qml矩形边框与内容重叠?,qml,Qml,我想要一个矩形来自动调整大小,使其完全适合其视觉子对象。如果没有边界,那么以下方法非常有效: Rectangle { width: childrenRect.width+(border.width*2) height: childrenRect.height+(border.width*2) ... } 但是,如果矩形有边框,子对象将与之重叠。我尝试将子对象包装到容器中(在下面的示例中为列),并使用锚定.margins将容器移到上方以忽略矩形的边框,但未成功 impo
Rectangle {
width: childrenRect.width+(border.width*2)
height: childrenRect.height+(border.width*2)
...
}
但是,如果矩形有边框,子对象将与之重叠。我尝试将子对象包装到容器中(在下面的示例中为列
),并使用锚定.margins
将容器移到上方以忽略矩形的边框,但未成功
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
visible: true
width: 600; height: 600
Rectangle {
id: rect
border.width : 20
border.color: "yellow"
clip: true
width: childrenRect.width+(border.width*2)
height: childrenRect.height+(border.width*2)
Column {
anchors.margins: rect.border.width // does not work
Text { height: 40; text: "FoooooooooooooooMumble" }
Text { height: 40; text: "Bar" }
Button { height: 40; text: "press me" }
}
}
}
有人能建议怎么做吗?对于
锚定。要使用边距,必须设置边框锚定(边距空间与这些锚定相对)。例如:
Column {
anchors.margins: rect.border.width
anchors.left: rect.left
anchors.top: rect.top
...
}
嗯,我知道了。“边距”定位相对于相应的边定位,如果边边距未定义,则忽略相应的“边距”。因此,如果设置了anchors.margins,那么也必须设置anchors.left和anchors.top(或者,我假设,设置了right和bottom)