本页内容

ScrollIndicator QML Type

垂直或水平的非交互式滚动指示器。更多...

Import Statement: import QtQuick.Controls
Inherits:

Control

属性

关联属性

详细说明

ScrollIndicator 是一个非交互式指示器,用于显示当前滚动位置。滚动指示器可以是vertical 或horizontal ,并可附加到任何Flickable 上,例如ListView 和GridView 。

Flickable {
    // ...
    ScrollIndicator.vertical: ScrollIndicator { }
}

将 ScrollIndicator 附加到 Flickable 上

注意:当将 ScrollIndicator(vertically 或horizontally )附加到 Flickable时 ,其几何形状和以下属性会自动设置并适时更新:

已附加的 ScrollIndicator 会将自身重新设置为目标 Flickable 的子元素。垂直附加的 ScrollIndicator 会根据 Flickable 的高度调整自身大小,并根据layout direction 位于 Flickable 的任一侧。水平附加的 ScrollIndicator 会根据 Flickable 的宽度调整自身大小,并位于 Flickable 的底部。 可以通过为附加的 ScrollIndicator 指定另一个父对象来禁用自动几何管理。例如,当需要将 ScrollIndicator 放置在裁剪 Flickable 之外时,此功能非常有用。以下示例演示了这一点:

Flickable {
    id: flickable
    clip: true
    // ...
    ScrollIndicator.vertical: ScrollIndicator {
        parent: flickable.parent
        anchors.top: flickable.top
        anchors.left: flickable.right
        anchors.bottom: flickable.bottom
    }
}

绑定水平和垂直滚动指示器的活动状态

默认情况下,水平和垂直滚动指示器之间不会共享active 状态。为了在向任一方向滚动时保持两个指示器均可见,请按照以下示例所示,在活动状态之间建立双向绑定:

Flickable {
    anchors.fill: parent

    contentWidth: parent.width * 2
    contentHeight: parent.height * 2

    ScrollIndicator.horizontal: ScrollIndicator { id: hbar; active: vbar.active }
    ScrollIndicator.vertical: ScrollIndicator { id: vbar; active: hbar.active }
}

非附加式滚动指示器

可以不使用附加属性 API 来创建 ScrollIndicator 的实例。当附加滚动指示器的行为无法满足需求,或者未使用Flickable 时,此方法非常有用。在下面的示例中,水平和垂直滚动指示器用于显示用户已滚动到文本的哪个位置(使用MouseArea 代替Flickable ):

Rectangle {
    id: frame
    clip: true
    width: 160
    height: 160
    border.color: "black"
    anchors.centerIn: parent

    Text {
        id: content
        text: "ABC"
        font.pixelSize: 169

        MouseArea {
            id: mouseArea
            drag.target: content
            drag.minimumX: frame.width - width
            drag.minimumY: frame.height - height
            drag.maximumX: 0
            drag.maximumY: 0
            anchors.fill: content
        }
    }

    ScrollIndicator {
        id: verticalIndicator
        active: mouseArea.pressed
        orientation: Qt.Vertical
        size: frame.height / content.height
        position: -content.y / content.height
        anchors { top: parent.top; right: parent.right; bottom: parent.bottom }
    }

    ScrollIndicator {
        id: horizontalIndicator
        active: mouseArea.pressed
        orientation: Qt.Horizontal
        size: frame.width / content.width
        position: -content.x / content.width
        anchors { left: parent.left; right: parent.right; bottom: parent.bottom }
    }
}

单独使用的滚动指示器(未关联任何属性)

委托对象大小的变化

委托对象大小的变化可能会导致 ScrollIndicator 在新委托对象加载到视图时“跳动”。因此,建议使用大小一致的委托对象。有关更多信息,请参阅Variable Delegate Size and Section Labels 。

另请参阅 ScrollBar 、《自定义 ScrollIndicator》以及《指示器控件》。

属性文档

active : bool

该属性在指示器处于活动状态时有效,即当关联的 Flickable 为moving 时。

在任何方向滚动时,均可保持both horizontal and vertical indicators visible 状态。

当滚动指示器为attached to a flickable 时,该属性会自动设置。

horizontal : bool [read-only, since QtQuick.Controls 2.3 (Qt 5.10)]

该属性用于指定滚动指示器是否为水平方向。

该属性自 QtQuick.Controls 2.3(Qt 5.10)起引入。

另请参阅 orientation 。

minimumSize : real [since QtQuick.Controls 2.4 (Qt 5.11)]

该属性存储指示器的最小尺寸,单位按0.0 - 1.0 进行缩放。

该属性在 QtQuick.Controls 2.4(Qt 5.11)中引入。

另请参阅 size 、visualSize 以及visualPosition 。

orientation : enumeration

该属性用于指定指示器的方向。

可能的值:

常量描述
Qt.Horizontal水平
Qt.Vertical垂直(默认)

当滚动指示器为attached to a flickable 时,此属性会自动设置。

另请参阅 horizontal 和vertical 。

position : real

该属性存储指示器的位置,并按0.0 - 1.0 进行缩放。

当滚动指示器为attached to a flickable 时,该属性会自动设置。

另请参阅 Flickable::visibleArea 和visualPosition 。

size : real

该属性存储指示器的尺寸,并按0.0 - 1.0 的比例进行缩放。

当滚动指示器为attached to a flickable 时,该属性会自动设置。

另请参阅 Flickable::visibleArea 、minimumSize 和visualSize 。

vertical : bool [read-only, since QtQuick.Controls 2.3 (Qt 5.10)]

该属性用于指定滚动指示器是否为垂直的。

该属性在 QtQuick.Controls 2.3(Qt 5.10)中引入。

另请参阅 orientation 。

visualPosition : real [since QtQuick.Controls 2.4 (Qt 5.11)]

该属性存储指示器的实际视觉位置,该位置可能会受到minimum size 的限制。

该属性在 QtQuick.Controls 2.4(Qt 5.11)中引入。

另请参阅 position 和minimumSize 。

visualSize : real [since QtQuick.Controls 2.4 (Qt 5.11)]

该属性存储指示器的实际视觉尺寸,该尺寸可能会受到minimum size 的限制。

该属性首次引入于 QtQuick.Controls 2.4(Qt 5.11)。

另请参阅 size 和minimumSize 。

附加属性文档

ScrollIndicator.horizontal : ScrollIndicator [attached]

此属性会在Flickable 上添加一个水平滚动指示器。

Flickable {
    contentWidth: 2000
    ScrollIndicator.horizontal: ScrollIndicator { }
}

另请参阅 Attaching ScrollIndicator to a Flickable 。

ScrollIndicator.vertical : ScrollIndicator [attached]

此属性会在Flickable 上添加一个垂直滚动指示器。

Flickable {
    contentHeight: 2000
    ScrollIndicator.vertical: ScrollIndicator { }
}

另请参阅 Attaching ScrollIndicator to a Flickable 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.