本页内容

Slider QML Type

用于通过沿轨道滑动滑块来选择一个值。更多...

Import Statement: import QtQuick.Controls
Inherits:

Control

属性

信号

  • moved() (since QtQuick.Controls 2.2 (Qt 5.9))

方法

详细说明

滑块用于通过沿轨道滑动手柄来选择数值。

在下面的示例中,设置了自定义的from 、value 和to 值:

Slider {
    from: 1
    value: 25
    to: 100
}

position 属性以控件大小的分数形式表示,范围为0.0 - 1.0 。visualPosition 属性与此相同,但在从右向左的应用程序中其方向是相反的。visualPosition 在为 Slider 设置样式时,有助于定位滑块控件。在上例中,visualPosition 在从左到右的应用程序中将变为0.24 ,而在从右到左的应用程序中将变为0.76 。

有关允许用户通过提供两个控点来选择范围的滑块,请参阅RangeSlider 。

另请参阅 《自定义滑块和 输入控件》。

属性文档

from : real

该属性存储范围的起始值。默认值为0.0 。

另请参阅 to 和value 。

handle : Item

该属性保存句柄项。

另请参阅 “自定义滑块”。

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

该属性用于确定滑块是否为水平滑块。

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

另请参阅 orientation 。

implicitHandleHeight : real [read-only, since QtQuick.Controls 2.5 (Qt 5.12)]

该属性存储隐式句柄的高度。

该值等于handle ? handle.implicitHeight : 0 。

通常,该属性与implicitContentHeight 和implicitBackgroundHeight 结合使用,以计算implicitHeight 。

该属性在 QtQuick.Controls 2.5(Qt 5.12)中引入。

另请参阅 implicitHandleWidth 。

implicitHandleWidth : real [read-only, since QtQuick.Controls 2.5 (Qt 5.12)]

该属性存储隐式句柄的宽度。

该值等于handle ? handle.implicitWidth : 0 。

通常,该属性与implicitContentWidth 和implicitBackgroundWidth 配合使用,以计算implicitWidth 。

该属性在 QtQuick.Controls 2.5(Qt 5.12)中引入。

另请参阅 implicitHandleHeight 。

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

该属性用于指定在拖动滑块手柄时,滑块是否为value 属性提供实时更新。

默认值为true 。

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

另请参阅 value 和valueAt()。

orientation : enumeration

该属性用于存储方向信息。

可能的取值:

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

另请参阅 horizontal 和vertical 。

position : real [read-only]

该属性存储控件的逻辑位置。

该位置以控件大小的分数形式表示,取值范围为0.0 - 1.0 。若要可视化滑块,应改用支持从右向左方向的visualPosition 属性。

另请参阅 value 、visualPosition 以及valueAt()。

pressed : bool

无论滑块是通过触摸、鼠标还是键盘按键被点击,此属性均保持有效。

snapMode : enumeration

该属性用于设置对齐模式。

对齐模式决定了滑块手柄相对于stepSize 的行为方式。

可能的取值:

常量描述
Slider.NoSnap滑块不进行对齐(默认)。
Slider.SnapAlways在拖动滑块手柄时,滑块会自动对齐。
Slider.SnapOnRelease滑块在拖动过程中不会对齐,仅在松开控件后才会对齐。

在下表中,通过动画演示了各种模式。每段动画中,鼠标光标的移动与stepSize (0.2 )完全一致。

值示例
Slider.NoSnap

Slider.SnapAlways

Slider.SnapOnRelease

另请参阅 stepSize 。

stepSize : real

该属性用于指定步长。默认值为0.0 。

另请参阅 snapMode 、increase() 和decrease()。

to : real

该属性存储了范围的结束值。默认值为1.0 。

另请参阅 from 和value 。

touchDragThreshold : real [since QtQuick.Controls 2.5 (Qt 5.12)]

该属性存储触发触摸拖动事件的阈值(以逻辑像素为单位)。鼠标拖动阈值不会受到影响。默认值为Application.styleHints.startDragDistance 。

该属性在 QtQuick.Controls 2.5(Qt 5.12)中引入。

另请参阅 QStyleHints 。

value : real

该属性的取值范围为from -to 。默认值为0.0 。

另请参阅 position 。

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

该属性用于指定滑块是否为垂直的。

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

另请参阅 orientation 。

visualPosition : real [read-only]

该属性存储了控制柄的视觉位置。

该位置以控件大小的分数形式表示,范围为0.0 - 1.0 。当控件为mirrored 时,该值等于1.0 - position 。这使得该值适合用于可视化滑块,同时考虑了从右到左的显示支持。

另请参阅 position 。

Signal 文档

[since QtQuick.Controls 2.2 (Qt 5.9)] moved()

当用户通过触摸、鼠标、滚轮或键盘交互式地移动滑块时,会触发此信号。

注意: 相应的处理函数 为onMoved 。

该信号在 QtQuick.Controls 2.2(Qt 5.9)中引入。

方法文档

void decrease()

若未定义 `stepSize `,则将该值减去 `stepSize ` 或 `0.1 `。

另请参阅 stepSize 。

void increase()

如果未定义 `stepSize `,则将该值增加 `stepSize ` 或 `0.1 `。

另请参阅 stepSize 。

[since QtQuick.Controls 2.1 (Qt 5.8)] real valueAt(real position)

返回给定position 的值。

该方法在 QtQuick.Controls 2.1(Qt 5.8)中引入。

另请参阅 value 和position 。

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