StyleReader QML Type
从特定控件的当前样式中读取属性。更多...
| Import Statement: | import Qt.labs.StyleKit |
| Inherits: | |
| Status: | Technology preview |
此类型处于技术预览阶段,可能会有变动。
属性
- checked : bool
- controlType : enumeration
- enabled : bool
- focused : bool
- font : font
- global : ControlStyleProperties
- highlighted : bool
- hovered : bool
- palette : palette
- pressed : bool
- vertical : bool
详细说明
StyleReader 是 Qt Quick Controls 与active style 之间的桥梁。它公开了所有样式属性——backgrounds 、indicators 、handles 、text 、padding 等——这些是控件及其委托应绑定的属性。所有内置的StyleKit 控件内部都为此目的使用了StyleReader。
状态属性——hovered 、pressed 、focused 、checked 、highlighted 等——用于告知 StyleReader 控件当前处于何种状态。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.width
implicitHeight: styleReader.background.height
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 以及《StyleKit 属性解析》。
属性文档
checked : bool
该控件是否已被选中。
将此属性绑定到控件的checked 属性。
默认值为false 。
另请参阅 ControlStateStyle.checked 。
controlType : enumeration
指定该读取器从Style 中的哪个控件类型读取属性。此参数可以设置为以下预定义值之一,也可以设置为在Style 中定义的custom control type 。
| 常量 | 描述 |
|---|---|
StyleReader.BusyIndicator | BusyIndicator |
StyleReader.Button | Button |
StyleReader.CheckBox | CheckBox |
StyleReader.CheckDelegate | CheckDelegate |
StyleReader.ComboBox | ComboBox |
StyleReader.DelayButton | DelayButton |
StyleReader.Dial | Dial |
StyleReader.Dialog | Dialog |
StyleReader.DialogButtonBox | DialogButtonBox |
StyleReader.Frame | Frame |
StyleReader.GroupBox | GroupBox |
StyleReader.ItemDelegate | ItemDelegate |
StyleReader.Label | Label |
StyleReader.Menu | Menu |
StyleReader.MenuBar | MenuBar |
StyleReader.MenuBarItem | MenuBarItem |
StyleReader.MenuItem | MenuItem |
StyleReader.MenuSeparator | MenuSeparator |
StyleReader.Page | Page |
StyleReader.PageIndicator | PageIndicator |
StyleReader.Pane | Pane |
StyleReader.Popup | Popup |
StyleReader.ProgressBar | ProgressBar |
StyleReader.RadioButton | RadioButton |
StyleReader.RadioDelegate | RadioDelegate |
StyleReader.RoundButton | RoundButton |
StyleReader.ScrollBar | ScrollBar |
StyleReader.ScrollView | ScrollView |
StyleReader.SearchField | SearchField |
StyleReader.Slider | Slider |
StyleReader.SpinBox | SpinBox |
StyleReader.SwipeDelegate | SwipeDelegate |
StyleReader.Switch | Switch |
StyleReader.SwitchDelegate | SwitchDelegate |
StyleReader.TabBar | TabBar |
StyleReader.TabButton | TabButton |
StyleReader.TextArea | TextArea |
StyleReader.TextField | TextField |
StyleReader.ToolBar | ToolBar |
StyleReader.ToolButton | ToolButton |
StyleReader.ToolSeparator | ToolSeparator |
另请参阅 StyleVariation.controlType 。
enabled : bool
该控件是否已启用。
将此属性绑定到控件的enabled 属性上。
默认值为true 。
另请参阅 ControlStateStyle.disabled 。
focused : bool
该控件是否处于活动焦点状态。
将此属性绑定到控件的activeFocus 属性上。
默认值为false 。
另请参阅 ControlStateStyle.focused 。
font : font [read-only]
该控件类型的有效字体,由style 定义。这同时也会考虑在样式中text 属性中设置的任何字体覆盖设置。
将控件的font 属性绑定到此属性。
注意:与 hovered 、pressed 和palette 等属性不同 ——这些属性是从控件转发到StyleReader 的——font是一个输出属性。请将控件的font 属性绑定到此值,而不是反过来。
global : ControlStyleProperties [read-only]
可直接访问样式属性,绕过任何正在进行的transition 。
在状态转换进行期间,从StyleReader 读取的样式属性可能会返回插值值。通过在属性路径前添加global ,您可以绕过转换过程,立即获取最终状态的值。
例如,在从hovered 过渡到pressed 时,background.color 可能会返回一个介于hovered 状态下的color 与pressed 状态下的颜色之间的插值值。而global.background.color 则会直接返回按下状态下的颜色。
另请参阅 transitionsEnabled 。
highlighted : bool
该控件是否处于高亮状态。
将此属性绑定到控件的highlighted 属性。
默认值为false 。
另请参阅 ControlStateStyle.highlighted 。
hovered : bool
控件是否处于悬停状态。
将此属性绑定到控件的hovered 属性上。
默认值为false 。
另请参阅 ControlStateStyle.hovered 。
palette : palette
控件的配色方案。StyleKit 会利用此配色方案来解析 `Style` 中绑定到 `palette ` 的颜色属性。
将其绑定到控件的 `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.