QML控件类型:ScrollBar、ScrollIndicator

ScrollBar

一、描述

ScrollBar 是滚动条控件,可用于滚动到特定位置。滚动条可以附加到任何 Flickable,例如 ListViewGridView

Flickable {
    // ...
    ScrollBar.vertical: ScrollBar { }
}

1.1、将 ScrollBar 附加到 Flickable

当 ScrollBar 垂直或水平附加到 Flickable 时,它的几何图形和以下属性会根据需要自动设置和更新:

  • orientation
  • position
  • size
  • active

垂直连接的 ScrollBar 将自身调整到 Flickable 的高度,并根据布局方向将自身定位到其任一侧。 水平连接的 ScrollBar 将自身调整为 Flickable 的宽度,并将自身定位到底部。

可以通过为附加的 ScrollBar 指定另一个父级来禁用自动几何管理。如:

    Flickable {
        id: flickable
        clip: true
        // ...
        ScrollBar.vertical: ScrollBar {
            parent: flickable.parent    //设置另一个父级
            anchors.top: flickable.top
            anchors.left: flickable.right
            anchors.bottom: flickable.bottom
        }
    }

ScrollBar 不会过滤它附加到的 Flickable 的按键事件。以下示例说明了如何使用向上和向下键实现滚动:

Flickable {
    focus: true

    Keys.onUpPressed: scrollBar.decrease()
    Keys.onDownPressed: scrollBar.increase()

    ScrollBar.vertical: ScrollBar { id: scrollBar }
}

1.2、绑定水平和垂直滚动条的活动状态

默认情况下,水平和垂直滚动条彼此不共享活动状态。为了在滚动到任一方向时保持两个栏可见,可在活动状态之间建立双向绑定,如下例所示:

Flickable {
    anchors.fill: parent

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

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

1.3、非附加滚动条

可以在不使用附加属性 API 的情况下创建 ScrollBar 的实例。当附加的滚动条的行为不充分或未使用 Flickable 时,这很有用。在下面的示例中,水平和垂直滚动条用于在不使用 Flickable 的情况下滚动文本:

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

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

        Text {
            id: content
            text: "ABC"
            font.pixelSize: 160
            x: -hbar.position * width
            y: -vbar.position * height
        }

        ScrollBar {
            id: vbar
            hoverEnabled: true
            active: hovered || pressed
            orientation: Qt.Vertical
            size: frame.height / content.height
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.bottom: parent.bottom
        }

        ScrollBar {
            id: hbar
            hoverEnabled: true
            active: hovered || pressed
            orientation: Qt.Horizontal
            size: frame.width / content.width
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.bottom: parent.bottom
        }
    }
}

使用非附加滚动条时,必须手动完成以下操作:

  1. 布局滚动条(例如,使用 x 和 y 或 anchors 属性)。
  2. 设置 size position 属性以确定滚动条相对于滚动项的大小和位置。
  3. 设置 active 属性以确定滚动条何时可见。

二、属性成员

1、active : bool

滚动条是否处于活动状态,即当它被按下或附加的 Flickable 正在移动时。

2、[read-only] horizontal : bool / [read-only] vertical : bool

滚动条是否水平 / 垂直。

      orientation : enumeration

滚动条方向。 

  • Qt.Horizontal
  • Qt.Vertical:默认。

3、interactive : bool

滚动条是否是交互式的。默认值是true。

非交互式滚动条在视觉和行为上类似于 ScrollIndicator

4、minimumSize : real

滚动条的最小尺寸,范围:0.0 - 1.0。

      size : real

滚动条的大小,范围:0.0 - 1.0。

      visualSize : real

滚动条的有效视觉尺寸,可能受 minimumSize 限制。

5、policy : enumeration

滚动条的策略。默认策略是 ScrollBar.AsNeeded

  • ScrollBar.AsNeeded:滚动条仅在内容太大而无法容纳时显示。
  • ScrollBar.AlwaysOff:始终不会显示滚动条。
  • ScrollBar.AlwaysOn:始终显示滚动条。 

6、position : real

滚动条的位置,范围是:0.0 - 1.0。

7、pressed : bool

滚动条是否被按下。

8、snapMode : enumeration

对齐模式。

  • ScrollBar.NoSnap:滚动条不对齐(默认)。
  • ScrollBar.SnapAlways:滚动条在拖动时对齐。
  • ScrollBar.SnapOnRelease:滚动条在拖动时不会对齐,只有在释放后才会对齐。

9、stepSize : real

步长。

10、visualPosition : real

滚动条的有效视觉位置,可能受 minimumSize 限制。

三、附加属性成员

1、ScrollBar.horizontal : ScrollBar 

此属性将水平滚动条附加到 Flickable

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

2、ScrollBar.vertical : ScrollBar

此属性将垂直滚动条附加到 Flickable

四、成员函数

1、void decrease()

stepSize 或 0.1 (未设置 stepSize 时)减小 position

2、void increase()

stepSize 或 0.1 (未设置 stepSize 时)增加 position


ScrollIndicator

一、描述

ScrollIndicator 是指示当前滚动位置的非交互式指示器。滚动指示器可以是垂直的也可以是水平的,并且可以附加到任何 Flickable,例如 ListView GridView。即它只指示拖动状态但不可交互,使用方式和 ScrollBar 一样。

1.1、非附加滚动指示器

可以在不使用附加属性 API 的情况下创建 ScrollIndicator 的实例。 当附加的 ScrollIndicator 的行为不充分或 Flickable 未使用时,这很有用。在以下示例中,水平和垂直滚动指示器用于指示用户在文本上滚动了多远(使用 MouseArea 而不是 Flickable):

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    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 }
        }
    }
}

二、属性成员

与 ScrollBar 比较,ScrollIndicator 没有交互相关的属性:

三、附加属性成员

与 ScrollBar 类似。

四、自定义 ScrollIndicator 示例

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    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 }

            contentItem: Rectangle {
                implicitWidth: 2
                color: "#ff0000"
            }

        }

        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 }

            contentItem: Rectangle {
                implicitHeight: 2
                color: "#ff0000"
            }
        }
    }
}

  • 12
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ScrollBar是一种滚动条控件,它可以用于实现在特定位置进行滚动的功能。它可以附加到任何Flickable(例如ListView和GridView)上。使用ScrollBar可以实现通过按键事件来进行滚动的功能。例如,可以通过向上和向下键来增加和减少滚动条的值。以下是一个示例代码: ``` Flickable { focus: true Keys.onUpPressed: scrollBar.decrease() Keys.onDownPressed: scrollBar.increase() ScrollBar.vertical: ScrollBar { id: scrollBar } } ``` 另外,还可以将ScrollBar附加到Flickable上来实现滚动的功能。以下是一个示例代码: ``` Flickable { // ... ScrollBar.vertical: ScrollBar { } } ``` 在上述代码中,ScrollBar被附加到Flickable上,这样就可以使用滚动条来实现垂直滚动的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [QML控件类型ScrollBarScrollIndicator](https://blog.csdn.net/kenfan1647/article/details/122522063)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [qml 滚动控件Scroll ScrollBar ScrollIndicator ScrollView](https://blog.csdn.net/qq_33373173/article/details/109759694)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值