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

QML中布局内的SplitView无法正常工作

解决布局内SplitView拖动后恢复的问题

问题出在SplitView与父布局的约束冲突:你给SplitView设置了anchors.fill: parent,但它的父容器是RowLayout——布局控件会自动管理子项的尺寸,锚定属性会和布局的约束产生冲突,导致拖动拆分条后,布局会强制重置SplitView的大小,回到初始状态。

修复方案

去掉SplitView的anchors.fill: parent,改用布局专属的Layout.fillWidth: true和Layout.fillHeight: true,让布局正确分配SplitView的空间:

ColumnLayout {
    anchors.fill: parent
    spacing: 20

    RowLayout {
        spacing: 20
        height: 500
 
        SplitView {
            // 替换anchors.fill为布局属性
            Layout.fillWidth: true
            Layout.fillHeight: true
            orientation: Qt.Horizontal

            Rectangle {
                SplitView.preferredWidth: 200
                SplitView.fillHeight: true
                color: "lightblue"
                Text { anchors.centerIn: parent; text: "View 1" }
            }
            Rectangle {
                id: centerItem
                SplitView.preferredWidth: 200
                SplitView.fillHeight: true
                color: "lightgray"
                Text { anchors.centerIn: parent; text: "View 2" }
            }
            Rectangle {
                SplitView.fillWidth: true
                SplitView.fillHeight: true
                color: "lightgreen"
                Text { anchors.centerIn: parent; text: "View 3" }
            }
        }
    }
}

核心原因

Qt Quick布局(如RowLayout、ColumnLayout)通过Layout.*属性控制子项的尺寸和位置,而anchors.fill会绕过布局的管理逻辑,导致两种尺寸约束互相冲突。当你拖动拆分条调整SplitView子项大小后,布局会重新计算并强制设置SplitView的原始尺寸,就出现了“恢复到最小宽度”的现象。改用布局属性后,SplitView的尺寸完全由布局管理,拆分条的调整也能被正确识别和保留。

内容的提问来源于stack exchange,提问作者Jeyanth Inba

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 06:22:05