Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqstylekitcontrolstate.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
8
9// ************* ControlStateStyle ****************
10
11/*!
12 \qmltype ControlStateStyle
13 \nativetype QQStyleKitControlState
14 \inqmlmodule Qt.labs.StyleKit
15 \inherits ControlStyleProperties
16 \brief Describes the style of a control in a given state.
17
18 ControlStateStyle describes the style of a control in a particular state.
19 A \l ControlStyle inherits ControlStateStyle, since it represents the
20 \e normal state — properties set directly on a \l ControlStyle describe
21 how the control looks when no other state is active. State-specific
22 overrides are then set through nested states such as \l pressed,
23 \l hovered, and \l checked.
24
25 Nested states are not mutually exclusive. Multiple states can be active at
26 the same time — for example, a button can be both hovered and
27 pressed simultaneously. When several states are active, all matching
28 state overrides are applied. If the same property is set in multiple
29 active states, conflicts are resolved using the following priority
30 order: \l pressed, \l hovered, \l highlighted, \l focused,
31 \l checked, \l vertical. So for example \c {pressed.background.color}
32 wins over \c {checked.background.color} if the control is both
33 \l pressed and \l checked.
34
35 The \l disabled state is an exception: a disabled control cannot
36 be interacted with, so the \l pressed, \l hovered, \l highlighted,
37 and \l focused states will not apply. However, \l disabled can
38 still be combined with states like \l checked and \l vertical.
39
40 The more deeply nested a state is, the more qualified it is.
41 For example, \c {hovered.pressed.background.color} takes precedence over
42 \c {hovered.background.color} when both \l hovered and \l pressed are
43 active. The nesting order does not matter:
44 \c {hovered.pressed} and \c {pressed.hovered} are equivalent.
45 However, if both forms are used at the same time, which one wins is
46 undefined.
47
48 Deeper nesting of states can also be used to resolve conflicts. If the same property
49 is set in both \l hovered and \l checked, the priority order means
50 the \l hovered value wins. If you would rather have the checked value win, or
51 use an altogether different value in that situation, you can override
52 the property in \c {hovered.checked}, which then takes precedence over both.
53
54 The following snippet shows how to style a button differently
55 depending on its state:
56
57 \snippet ControlStateStyle_states.qml States
58
59 \labs
60
61 \sa ControlStyle, DelegateStyle, {StyleKit Property Resolution},
62 {Reference Fallback Style}
63*/
64
65/*!
66 \qmlproperty ControlStateStyle ControlStateStyle::pressed
67
68 Style overrides applied when the control is \l {StyleReader::}{pressed}.
69
70 \sa hovered, highlighted, focused, checked, vertical, disabled,
71 {StyleReader::pressed}{StyleReader.pressed}
72*/
73
74/*!
75 \qmlproperty ControlStateStyle ControlStateStyle::hovered
76
77 Style overrides applied when the control is \l {StyleReader::}{hovered}.
78
79 \sa pressed, highlighted, focused, checked, vertical, disabled,
80 {StyleReader::hovered}{StyleReader.hovered}
81*/
82
83/*!
84 \qmlproperty ControlStateStyle ControlStateStyle::highlighted
85
86 Style overrides applied when the control is \l {StyleReader::}{highlighted}.
87
88 \sa pressed, hovered, focused, checked, vertical, disabled,
89 {StyleReader::highlighted}{StyleReader.highlighted}
90*/
91
92/*!
93 \qmlproperty ControlStateStyle ControlStateStyle::focused
94
95 Style overrides applied when the control is \l {StyleReader::}{focused}.
96
97 \sa pressed, hovered, highlighted, checked, vertical, disabled,
98 {StyleReader::focused}{StyleReader.focused}
99*/
100
101/*!
102 \qmlproperty ControlStateStyle ControlStateStyle::checked
103
104 Style overrides applied when the control is \l {StyleReader::}{checked}.
105
106 \sa pressed, hovered, highlighted, focused, vertical, disabled,
107 {StyleReader::checked}{StyleReader.checked}
108*/
109
110/*!
111 \qmlproperty ControlStateStyle ControlStateStyle::vertical
112
113 Style overrides applied when the control is \l {StyleReader::}{vertical}
114 (for example, a vertical \l Slider or \l ScrollBar).
115
116 \sa pressed, hovered, highlighted, focused, checked, disabled,
117 {StyleReader::vertical}{StyleReader.vertical}
118*/
119
120/*!
121 \qmlproperty ControlStateStyle ControlStateStyle::disabled
122
123 Style overrides applied when the control is not \l {StyleReader::}{enabled}.
124
125 A disabled control cannot be interacted with, so \l pressed,
126 \l hovered, \l highlighted, and \l focused will not be applied
127 at the same time as disabled.
128
129 \sa pressed, hovered, highlighted, focused, checked, vertical,
130 {StyleReader::enabled}{StyleReader.enabled}
131*/
132
134 : QQStyleKitControlProperties(QQSK::PropertyGroup::Control, parent)
135{
136}
137
139{
140 if (m_nestedState == QQSK::StateFlag::Normal) {
141 Q_ASSERT(qobject_cast<const QQStyleKitControl *>(this));
142 auto *self = const_cast<QQStyleKitControlState *>(this);
143 return static_cast<QQStyleKitControl *>(self);
144 }
145 Q_ASSERT(qobject_cast<const QQStyleKitControl *>(parent()));
146 return static_cast<QQStyleKitControl *>(parent());
147}
148
149QQStyleKitControlState *QQStyleKitControlState::lazyCreateState(QQSK::StateFlag state) const
150{
151 if (m_nestedStateObjects.contains(state))
152 return m_nestedStateObjects.value(state);
153
154 QQStyleKitControlState *stateObj = new QQStyleKitControlState(control());
155 stateObj->m_nestedState = m_nestedState;
156 stateObj->m_nestedState.setFlag(state);
157 stateObj->m_nestedState.setFlag(QQSK::StateFlag::Normal, false);
158
159 auto *self = const_cast<QQStyleKitControlState *>(this);
160 self->m_nestedStateObjects.insert(state, stateObj);
161
162 return stateObj;
163}
164
166{
167 return lazyCreateState(QQSK::StateFlag::Pressed);
168}
169
171{
172 return lazyCreateState(QQSK::StateFlag::Hovered);
173}
174
176{
177 return lazyCreateState(QQSK::StateFlag::Highlighted);
178}
179
181{
182 return lazyCreateState(QQSK::StateFlag::Focused);
183}
184
186{
187 return lazyCreateState(QQSK::StateFlag::Checked);
188}
189
191{
192 return lazyCreateState(QQSK::StateFlag::Vertical);
193}
194
196{
197 return lazyCreateState(QQSK::StateFlag::Disabled);
198}
199
200QT_END_NAMESPACE
201
202#include "moc_qqstylekitcontrolstate_p.cpp"
\inmodule QtCore
Definition qobject.h:106
QQStyleKitControlState * checked() const
QQStyleKitControlState * hovered() const
QQStyleKitControlState * vertical() const
QQStyleKitControl * control() const
QQStyleKitControlState * disabled() const
QQStyleKitControlState * focused() const
QQStyleKitControlState * pressed() const
QQStyleKitControlState * highlighted() const
Combined button and popup list for selecting options.