我不明白为什么我的 ToolButton 这么小。工具栏是顶部的橙色矩形。以下代码:
Item{
ToolBar{
id: toolbar
width: parent.width
height: scaleDP(0.29)
anchors.top: parent.top
background: Rectangle{
color: "orange"
}
RowLayout{
anchors.fill: parent
ToolButton {
id: buttonBack
height: parent.height
width: parent.height
Image {
id: imageBack
source: "qrc:/assets/images/left-filled-black-50.png"
anchors.fill: parent
anchors.margins: 4
}
}
显示我的 ToolButton 太小了:
我可以更改图像的高度并使其变大,但随后它会超出 ToolButton。当我尝试调整 ToolButton 的高度时,没有任何反应
调查结果
问题似乎出在 RowLayout 上。如果我将其更改为行或矩形,则图标会按预期调整大小。
最佳答案
RowLayout 使用其子项的 implicitHeight 和 implicitWidth,并忽略高度和宽度。对于一些简单的项目(例如矩形),设置它们的高度也会改变它们的隐式高度,但对于像工具按钮这样的快速控件,情况并非如此。
RowLayout{
height: 20
width: parent.width
// WON'T WORK
ToolButton {
id: buttonBack1
height: parent.height
width: parent.height
}
// OK
ToolButton {
id: buttonBack2
Layout.preferredHeight: parent.height
Layout.preferredWidth: parent.height
}
// OK TOO
ToolButton {
id: buttonBack3
implicitHeight: parent.height
implicitWidth: parent.height
}
}
如果您仍然需要 RowLayout
,您有两个选择:
- 设置 implicitHeigt 和 implicitWidth 或者
- 使用布局的附加属性:
Layout.preferredHeight
&Layout.preferredWidth
关于qt - QML ToolButton 不调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48143935/