QML - 设置宽度和高度无效
QML - setting width and height has no effect
快速控制 2,Qt 5.10。
我根据 ListView
项创建了 table 控件。
其中一列使用此组件显示:
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
Item
{
id: root
implicitHeight: item1.implicitHeight
ColumnLayout
{
id: item1
visible: !model.finished
width: parent.width
RowLayout
{
Label
{
text: "38%"
Layout.alignment: Qt.AlignLeft
}
Label
{
text: "Paused"
Layout.alignment: Qt.AlignRight
}
}
ProgressBar
{
from: 0; to: 100; value: 40
// Variant A
/*Layout.preferredWidth: 30
Layout.preferredHeight: 10*/
// Variant B
width: 30
height: 10
}
}
}
有人可以解释一下为什么变体 B 没有 "work"。我可以指定任何 width/height 值,甚至只是删除它们 - 没有效果。变体 A (Layout.preferredWidth/Layout.preferredHeight) 工作正常。
变体 A:
变体 B:
...Layout
项会改变其子项的尺寸。这就是他们的目的,并且记录了行为。
根据 ColumnLayout
Layout.preferredWidth
的文档,行为是:
This property holds the preferred width of an item in a layout. If the preferred width is -1 it will be ignored, and the layout will use implicitWidth instead. The default is -1.
因为默认是-1,所以会取implicitWidth
——不是写成"and use width instead".
如果您不想使用 Layout
,请不要使用 Layout
。您可以只取 Column
。
快速控制 2,Qt 5.10。
我根据 ListView
项创建了 table 控件。
其中一列使用此组件显示:
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
Item
{
id: root
implicitHeight: item1.implicitHeight
ColumnLayout
{
id: item1
visible: !model.finished
width: parent.width
RowLayout
{
Label
{
text: "38%"
Layout.alignment: Qt.AlignLeft
}
Label
{
text: "Paused"
Layout.alignment: Qt.AlignRight
}
}
ProgressBar
{
from: 0; to: 100; value: 40
// Variant A
/*Layout.preferredWidth: 30
Layout.preferredHeight: 10*/
// Variant B
width: 30
height: 10
}
}
}
有人可以解释一下为什么变体 B 没有 "work"。我可以指定任何 width/height 值,甚至只是删除它们 - 没有效果。变体 A (Layout.preferredWidth/Layout.preferredHeight) 工作正常。
变体 A:
变体 B:
...Layout
项会改变其子项的尺寸。这就是他们的目的,并且记录了行为。
根据 ColumnLayout
Layout.preferredWidth
的文档,行为是:
This property holds the preferred width of an item in a layout. If the preferred width is -1 it will be ignored, and the layout will use implicitWidth instead. The default is -1.
因为默认是-1,所以会取implicitWidth
——不是写成"and use width instead".
如果您不想使用 Layout
,请不要使用 Layout
。您可以只取 Column
。