在本页

Tumbler QML Type

可选择项目的可旋转轮盘。更多

Import Statement: import QtQuick.Controls
Inherits:

Control

属性

附属物业

方法

  • void positionViewAtIndex(int index, PositionMode mode) (since QtQuick.Controls 2.5 (Qt 5.12))

详细说明

Tumbler {
    model: 5
    // ...
}

Tumbler 允许用户从可旋转的项目"轮盘 "中选择一个选项。当选项过多无法使用RadioButton ,而选项过少需要使用可编辑的SpinBox 时,它就非常有用。它的方便之处在于不需要使用键盘,而且在有大量项目时,可以在两端缠绕。

其 API 与ListViewPathView 等视图类似;可以设置modeldelegate 属性,而countcurrentItem 属性提供了对视图信息的只读访问。要将视图定位在某个索引处,请使用positionViewAtIndex() 。

然而,与PathViewListView 等视图不同,视图始终有一个当前项(当模型不是空的时)。这意味着当count 等于0 时,currentIndex 将是-1 。在所有其他情况下,它将大于或等于0

默认情况下,只要模型中的条目多于可见条目,Tumbler 就会在到达顶部和底部时wraps ;也就是说,当count 大于visibleItemCount 时:

import QtQuick
import QtQuick.Window
import QtQuick.Controls

Rectangle {
    width: frame.implicitWidth + 10
    height: frame.implicitHeight + 10

    FontMetrics {
        id: fontMetrics
    }

    Component {
        id: delegateComponent

        Label {
            text: formatText(Tumbler.tumbler.count, modelData)
            opacity: 1.0 - Math.abs(Tumbler.displacement) / (Tumbler.tumbler.visibleItemCount / 2)
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: fontMetrics.font.pixelSize * 1.25

            function formatText(count, modelData) {
                var data = count === 12 ? modelData + 1 : modelData;
                return data.toString().length < 2 ? "0" + data : data;
            }
        }
    }

    Frame {
        id: frame
        padding: 0
        anchors.centerIn: parent

        Row {
            id: row

            Tumbler {
                id: hoursTumbler
                model: 12
                delegate: delegateComponent
            }

            Tumbler {
                id: minutesTumbler
                model: 60
                delegate: delegateComponent
            }

            Tumbler {
                id: amPmTumbler
                model: ["AM", "PM"]
                delegate: delegateComponent
            }
        }
    }
}

另请参阅 自定义滚动条输入控件

属性文档

count : int [read-only]

此属性表示模型中项目的数量。

currentIndex : int

该属性保存当前项目的索引。

count 等于0 时,该属性的值为-1 。在所有其他情况下,该值将大于或等于0

另请参阅 currentItempositionViewAtIndex()。

currentItem : Item [read-only]

该属性保存当前索引中的项目。

另请参阅 currentIndexpositionViewAtIndex()。

delegate : Component

此属性包含用于显示每个项目的委托。

Tumbler 不拥有该委托的所有权。

flickDeceleration : int

该属性表示滑动减速的速度:数字越大,当用户停止触摸滑动时,减速速度越快。例如,0.0001 几乎 "无摩擦",而10000 感觉相当 "粘"。

wrap 为真时(默认值),默认的 flickDeceleration 为100 。否则,将取决于平台。要覆盖此行为,请明确设置此属性的值。要返回默认行为,请将此属性设置为未定义。不允许使用 0 或更小的值。

model : variant

该属性包含为该滚揉机提供数据的模型。

moving : bool [since QtQuick.Controls 2.2 (Qt 5.9)]

该属性描述滚揉器当前是否由于用户拖动或轻弹而移动。

该属性在 QtQuick.Controls 2.2 (Qt 5.9) 中引入。

visibleItemCount : int

此属性表示滚揉器中可见的项目数。它必须是奇数,因为当前项目总是垂直居中。

wrap : bool [since QtQuick.Controls 2.1 (Qt 5.8)]

该属性决定不倒翁在到达顶部或底部时是否缠绕。

count 小于visibleItemCount 时,默认值为false ,因为当只有少量物品时,与不缠绕滚揉器交互更简单。要覆盖此行为,请明确设置此属性的值。要返回默认行为,请将此属性设置为undefined

此属性在 QtQuick.Controls 2.1 (Qt 5.8) 中引入。

附加属性文档

Tumbler.displacement : real [read-only]

该附加属性持有一个从-visibleItemCount / 2visibleItemCount / 2 的值,表示该项目距离当前项目的距离,其中0 表示完全当前。

例如,当下面的项目不是当前项目时,不透明度为 40%,而当它成为当前项目时,不透明度将过渡到 100%:

delegate: Text {
    text: modelData
    opacity: 0.4 + Math.max(0, 1 - Math.abs(Tumbler.displacement)) * 0.6
}

Tumbler.tumbler : Tumbler [read-only]

该附加属性用于保存不倒翁。该属性可附加到不倒翁委托。如果项目不是不倒翁委托,则值为null

方法文档

[since QtQuick.Controls 2.5 (Qt 5.12)] void positionViewAtIndex(int index, PositionMode mode)

定位视图,使index 位于mode 指定的位置。

例如

positionViewAtIndex(10, Tumbler.Center)

如果wrap 为 true(默认值),则可使用PathViewpositionViewAtIndex() 函数提供的模式,否则可使用ListViewpositionViewAtIndex() 函数提供的模式。

注意: 有一个已知的限制,即当wraptrue 时使用Tumbler.Beginning 会导致错误的项目被放置在视图顶部。作为一种变通方法,可通过index - 1

此方法在 QtQuick.Controls 2.5 (Qt 5.12) 中引入。

另请参阅 currentIndex

© 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.