You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QML ApplicationWindow:如何让内容严格置于页眉与页脚之间?

解决Qt Quick ApplicationWindow中ListView位于Header和Footer之间的问题

方法一:利用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 22:39:56