QML TabView in ColumnLayout
我正在尝试修改Gallery示例。我想在TabView下添加Button。因此,我将TabView和Button放入ColumnLayout,这是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.1
import QtQuick.Window 2.0
Window {
visible: true
title:"settings"
width: 600
height: 400
ColumnLayout{
anchors.fill: parent
TabView {
anchors.right: parent.right
anchors.left: parent.left
Tab {
title:"Controls"
Controls { }
}
Tab {
title:"Itemviews"
Controls { }
}
Tab {
title:"Styles"
Controls { }
}
Tab {
title:"Layouts"
Controls { }
}
}
RowLayout{
anchors.right: parent.right
anchors.left: parent.left
Button{
text:"ok"
}
}
}
} |
但是,当我调整窗口大小时,okButton会位于选项卡控件下。我应该如何修复代码?
- 正如我所见,您将Button置于TabView下,那么您有什么问题?我不能仅仅因为未定义的Controls来测试您的示例,但是也许您应该使用Layout.fillHeight
-
定义Layout后,添加的每个元素都可以访问与布局本身相关的特定属性。这些属性对于将元素放置在布局所覆盖的空间内很有用。面对这里描述的内容。
因此,您应该像这样修改ColumnLayout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| ColumnLayout {
anchors.fill: parent
TabView {
id:frame
enabled: enabledCheck.checked
tabPosition: controlPage.item ? controlPage.item.tabPosition : Qt.TopEdge
Layout.fillHeight: true // fill the available space vertically
Layout.fillWidth: true // fill the available space horizontally
Layout.row: 0 // item in the first row of the column
anchors.margins: Qt.platform.os ==="osx" ? 12 : 2
Tab {
id: controlPage
title:"Controls"
Controls { }
}
Tab {
title:"Itemviews"
ModelView { }
}
Tab {
title:"Styles"
Styles { anchors.fill: parent }
}
Tab {
title:"Layouts"
Layouts { anchors.fill:parent }
}
}
Button {
text:"ok"
Layout.row: 1 // item in the second row of the column
Layout.alignment: Qt.AlignCenter // simple center the button in its spatial slot
}
} |
该按钮不需要RowLayout。由于它是一个简单的组件,因此应放置在已定义的ColumnLayout的第二行中。如果同一行上有多个元素,例如,子布局可能很有用。两个或更多按钮。
还请注意,锚定仅用于ColumnLayout以"拉伸"并适合窗口。所有其他操作都通过布局属性执行。有关一般规则,请参阅另一篇文章。
定义Layout后,添加的每个元素都可以访问与布局本身相关的特定属性。这些属性对于将元素放置在布局所覆盖的空间内很有用。面对这里描述的内容。
因此,您应该像这样修改ColumnLayout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| ColumnLayout {
anchors.fill: parent
TabView {
id:frame
enabled: enabledCheck.checked
tabPosition: controlPage.item ? controlPage.item.tabPosition : Qt.TopEdge
Layout.fillHeight: true // fill the available space vertically
Layout.fillWidth: true // fill the available space horizontally
Layout.row: 0 // item in the first row of the column
anchors.margins: Qt.platform.os ==="osx" ? 12 : 2
Tab {
id: controlPage
title:"Controls"
Controls { }
}
Tab {
title:"Itemviews"
ModelView { }
}
Tab {
title:"Styles"
Styles { anchors.fill: parent }
}
Tab {
title:"Layouts"
Layouts { anchors.fill:parent }
}
}
Button {
text:"ok"
Layout.row: 1 // item in the second row of the column
Layout.alignment: Qt.AlignCenter // simple center the button in its spatial slot
}
} |
该按钮不需要RowLayout。由于它是一个简单的组件,因此应放置在已定义的ColumnLayout的第二行中。如果同一行上有多个元素,例如,子布局可能很有用。两个或更多按钮。
还请注意,锚定仅用于ColumnLayout以"拉伸"并适合窗口。所有其他操作都通过布局属性执行。有关一般规则,请参阅另一篇文章。
- 当我调整窗口大小时,okButton置于选项卡的控件上方
-
如果按原样使用Gallery示例,并且按照Ive的方式更改代码,则不应该这样做。如果您删除了可能的最小宽度:这取决于制表符组件的设置。那是一个完全不同的问题。确实Controls具有Layout.minimumWidth: implicitWidth ...您不能仅仅调整Window的大小而无需考虑组件的布局。
-
我增加了minimumWidth和Height。现在工作正常
-
而已。 :)我建议阅读Ive提供的链接。他们将澄清您可能有的任何疑问。