在本页

StyleReader QML Type

从特定控件的活动样式中读取属性。更多

Import Statement: import Qt.labs.StyleKit
Inherits:

ControlStyleProperties

属性

详细说明

StyleReader 是Qt Quick Controls中的控件与active style 之间的桥梁。它公开了所有样式属性 -backgrounds,indicators,handles,text,padding 等 - 控件及其委托应绑定到这些属性。所有内置的StyleKit 控件内部都使用样式读取器来实现这一目的。

状态属性 -hovered,pressed,focused,checked,highlighted 等 - 告诉 StyleReader 控件处于哪种状态。它使用这些属性来解析并从Style 读取正确的属性值,同时考虑到Themes,StyleVariations,fallback types 以及属性传播。

在实现CustomControl 时,可以采用与内置控件相同的方法:

// Main.qml

component CustomButton : Rectangle {
    StyleReader {
        id: styleReader
        controlType: StyleReader.Button
        hovered: hoverHandler.hovered
        pressed: tapHandler.pressed
        palette: app.palette
    }

    implicitWidth: styleReader.background.implicitWidth
    implicitHeight: styleReader.background.implicitHeight
    color: styleReader.background.color
    radius: styleReader.background.radius

    HoverHandler { id: hoverHandler }
    TapHandler { id: tapHandler }

    Text {
        font: styleReader.font
        anchors.centerIn: parent
        text: "ok"
    }
}

注: ControlStyleProperties 继承的样式属性并不直接映射到Style 。相反,它们反映的是活动transition 期间可能插值的缓存。向它们写入内容不会更新Style 中的相应属性,并可能干扰正在进行的转换。需要更改样式属性时,请直接写入Style

注意: Qt.labs 模块中的类型不保证在未来版本中保持兼容。

另请参阅 CustomControl,Style, 和StyleVariation.controlType

属性文档

checked : bool

控件是否被选中。

将其绑定到控件的checked 属性。

默认值为false

另请参阅 ControlStateStyle.checked

controlType : enumeration

标识该阅读器从Style 中的哪种控件类型读取属性。该值可以设置为以下预定义值之一,也可以设置为Style 中定义的custom control type

常量常量
StyleReader.ButtonButton
StyleReader.CheckBoxCheckBox
StyleReader.ComboBoxComboBox
StyleReader.FrameFrame
StyleReader.GroupBoxGroupBox
StyleReader.ItemDelegateItemDelegate
StyleReader.LabelLabel
StyleReader.MenuMenu
StyleReader.DialogDialog
StyleReader.PagePage
StyleReader.PanePane
StyleReader.PopupPopup
StyleReader.ProgressBarProgressBar
StyleReader.RadioButtonRadioButton
StyleReader.ScrollBarScrollBar
StyleReader.ScrollViewScrollView
StyleReader.SearchFieldSearchField
StyleReader.SliderSlider
StyleReader.SpinBoxSpinBox
StyleReader.SwitchSwitch
StyleReader.TabBarTabBar
StyleReader.TabButtonTabButton
StyleReader.TextAreaTextArea
StyleReader.TextFieldTextField
StyleReader.ToolBarToolBar
StyleReader.ToolButtonToolButton
StyleReader.ToolSeparatorToolSeparator

另请参阅 StyleVariation.controlType

enabled : bool

控件是否启用。

将其绑定到控件的enabled 属性。

默认值为true

另请参阅 ControlStyleState.disabled。

focused : bool

控件是否有活动焦点。

将其绑定到控件的activeFocus 属性。

默认值为false

另请参阅 ControlStateStyle.focused

font : font [read-only]

该控件类型的有效字体,由style 定义。这也考虑了在text 样式属性中设置的任何字体覆盖。

将控件的font 属性绑定到此属性。

注意: hoveredpressedpalette 等属性不同(这些属性从控件转发到StyleReader ),字体是一个输出。将控件的font 属性绑定到该值,而不是相反。

global : ControlStyleProperties [read-only]

直接访问样式属性,绕过任何正在进行的transition

当状态转换正在进行时,从StyleReader 读取的样式属性可能会返回插值。通过在属性路径前加上global ,就可以绕过过渡,立即获得结束状态值。

例如,当从hovered 过渡到pressed 时,background.color 可能会返回hovered 状态下的colorpressed 状态下的颜色之间的插值。global.background.color另一方面, 会直接返回按压状态下的颜色。

另请参见 transitionsEnabled

highlighted : bool

控件是否高亮显示。

将其绑定到控件的highlighted 属性。

默认值为false

另请参阅 ControlStateStyle.highlighted

hovered : bool

控件是否悬停。

将此绑定到控件的hovered 属性。

默认值为false

另请参阅 ControlStateStyle.hovered

palette : palette

控件的调色板。StyleKit 使用它来解析绑定Stylepalette 的颜色属性。

将其绑定到控件的palette 属性。

另请参阅 Style.palette

pressed : bool

控件是否被按下。

将此绑定到控件的pressed 属性。

默认值为false

另请参阅 ControlStateStyle.pressed

vertical : bool

控件是否垂直放置。

将其绑定到控件的orientation

默认值为false

另请参阅 ControlStateStyle.vertical

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