QML ScrollView不滚动

aar*_*ich 6 qt qml

我需要使用 QML 创建一个长表单。该表单无法容纳在窗口内,因此我需要它可以滚动。但是我无法让滚动视图工作。这是我的问题的最小工作示例:

import QtQuick.Window 2.2
import QtQuick 2.9
import QtQuick.Controls 2.3

Window {
    visible: true
    width: 1280
    height: 720
    title: qsTr("Hello World")

    Rectangle{
        anchors.centerIn: parent
        width: parent.width*0.8;
        height: parent.height*0.7;

        ScrollView {
            anchors.fill: parent
            clip: true
            contentHeight: parent.height

            Rectangle{
                id: rect1
                width: parent.width
                height: 200
                color: "#ffff00"
                anchors.horizontalCenter: parent.horizontalCenter
            }

            Rectangle{
                id: rect2
                width: parent.width
                height: 500
                color: "#ff00ff"
                anchors.top: rect1.bottom
                anchors.horizontalCenter: parent.horizontalCenter
            }


            Rectangle{
                id: rect3
                width: parent.width
                height: 500
                color: "#00ffff"
                anchors.top: rect2.bottom
                anchors.horizontalCenter: parent.horizontalCenter
            }

        }

    }


}
Run Code Online (Sandbox Code Playgroud)

据我了解,这应该允许我滚动以便看到 3 个矩形。但是我只能看到第一个和第二个的上半部分,并且无法滚动。

任何帮助将不胜感激

tal*_*aki 7

由于 ScrollView 包含多个项目,因此您需要自行调整大小并将contentHeight 显式设置所有项目的组合高度。

为了进行测试,您可以将垂直滚动条设置为始终打开,以查看内容高度如何影响滚动条。

我注释掉了水平中心锚定,因为不需要它(矩形的宽度是滚动视图宽度)。

ScrollView {
    anchors.fill: parent
    clip: true
    ScrollBar.vertical.policy: ScrollBar.AlwaysOn
    contentHeight: rect1.height+rect2.height+rect3.height

    Rectangle{
        id: rect1
        width: parent.width
        height: 200
        color: "#ffff00"
        //anchors.horizontalCenter: parent.horizontalCenter
    }

    Rectangle{
        id: rect2
        width: parent.width
        height: 500
        color: "#ff00ff"
        anchors.top: rect1.bottom
        //anchors.horizontalCenter: parent.horizontalCenter
    }


    Rectangle{
        id: rect3
        width: parent.width
        height: 500
        color: "#00ffff"
        anchors.top: rect2.bottom
        //anchors.horizontalCenter: parent.horizontalCenter
    }

}
Run Code Online (Sandbox Code Playgroud)

如果您用一个项目包裹矩形并将项目设置implicitHeight为其高度,ScrollView 会正确检测 contentHeight。

ScrollView {
    anchors.fill: parent
    clip: true
    ScrollBar.vertical.policy: ScrollBar.AlwaysOn
    Item {
        width: parent.width
        height: rect1.height+rect2.height+rect3.height
        implicitHeight: height
        Rectangle{
            id: rect1
            width: parent.width
            height: 200
            color: "#ffff00"
        }
        Rectangle{
            id: rect2
            width: parent.width
            height: 500
            color: "#ff00ff"
            anchors.top: rect1.bottom
        }
        Rectangle{
            id: rect3
            width: parent.width
            height: 500
            color: "#00ffff"
            anchors.top: rect2.bottom
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

大多数项目的默认隐式大小是 0x0,这就是为什么您必须显式设置项目的隐式高度。然而,某些项目具有固有的隐式大小,例如图像和文本。这意味着,如果您将 TextArea 放入 ScrollView 中,如果文本足够长,它将自动变得可滚动。

ScrollView {
    anchors.fill: parent
    clip: true
    TextArea {
        readOnly: true
        text: online ? provider.loadedText : "Offline"
        wrapMode: Text.WordWrap
    }
}
Run Code Online (Sandbox Code Playgroud)