QML ApplicationWindow:如何让内容严格置于页眉与页脚之间?
方法一:利用ApplicationWindow的contentItem(推荐)
ApplicationWindow自带的contentItem区域已自动避开header和footer的位置,直接将ListView填充到该区域即可:
ApplicationWindow { header: MyHeader { id: myHeader } footer: MyFooter { id: myFooter } ListView { id: myListView anchors.fill: parent.contentItem } }
方法二:手动锚定四个边缘
若需手动控制锚定,需同时设置上下左右四个锚点——之前的尝试存在写法错误,且未补全锚点:
ApplicationWindow { header: MyHeader { id: myHeader } footer: MyFooter { id: myFooter } ListView { id: myListView anchors.top: myHeader.bottom anchors.bottom: myFooter.top anchors.left: parent.left anchors.right: parent.right } }
为什么之前的尝试无效?
Layout.fillWidth/Layout.fillHeight是RowLayout、ColumnLayout等布局容器的专属属性,直接在ApplicationWindow子项中使用不会生效。- 仅设置
anchors.top和anchors.bottom却未设置左右锚点,ListView宽度无法自动填充父项;且anchors.right: parent.width是错误写法,正确应为anchors.right: parent.right。 - 直接
anchors.fill: parent会让ListView覆盖整个ApplicationWindow(包含header和footer区域),导致内容被遮挡。
内容的提问来源于stack exchange,提问作者Dibo
相关产品推荐
相关产品推荐

