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 \inqmlmodule Qt.labs.StyleKit
14 \inherits ControlStyleProperties
15 \brief Describes the style of a control in a given state.
16
17 ControlStateStyle describes the style of a control in a particular state.
18 A \l ControlStyle inherits ControlStateStyle, since it represents the
19 \e normal state — properties set directly on a \l ControlStyle describe
20 how the control looks when no other state is active. State-specific
21 overrides are then set through nested states such as \l pressed,
22 \l hovered, and \l checked.
23
24 Nested states are not mutually exclusive. Multiple states can be active at
25 the same time — for example, a button can be both hovered and
26 pressed simultaneously. When several states are active, all matching
27 state overrides are applied. If the same property is set in multiple
28 active states, conflicts are resolved using the following priority
29 order: \l pressed, \l hovered, \l highlighted, \l focused,
30 \l checked, \l vertical. So for example \c {pressed.background.color}
31 wins over \c {checked.background.color} if the control is both
32 \l pressed and \l checked.
33
34 The \l disabled state is an exception: a disabled control cannot
35 be interacted with, so the \l pressed, \l hovered, \l highlighted,
36 and \l focused states will not apply. However, \l disabled can
37 still be combined with states like \l checked and \l vertical.
38
39 The more deeply nested a state is, the more qualified it is.
40 For example, \c {hovered.pressed.background.color} takes precedence over
41 \c {hovered.background.color} when both \l hovered and \l pressed are
42 active. The nesting order does not matter:
43 \c {hovered.pressed} and \c {pressed.hovered} are equivalent.
44 However, if both forms are used at the same time, which one wins is
45 undefined.
46
47 Deeper nesting of states can also be used to resolve conflicts. If the same property
48 is set in both \l hovered and \l checked, the priority order means
49 the \l hovered value wins. If you would rather have the checked value win, or
50 use an altogether different value in that situation, you can override
51 the property in \c {hovered.checked}, which then takes precedence over both.
52
53 The following snippet shows how to style a button differently
54 depending on its state:
55
56 \snippet ControlStateStyle_states.qml States
57
58 \labs
59
60 \sa ControlStyle, DelegateStyle,
61 {qtlabsstylekit-fallbackstyle.html}{FallbackStyle Reference}
62*/
63
64/*!
65 \qmlproperty ControlStateStyle ControlStateStyle::pressed
66
67 Style overrides applied when the control is \l {StyleReader::}{pressed}.
68
69 \sa hovered, highlighted, focused, checked, vertical, disabled,
70 {StyleReader::pressed}{StyleReader.pressed}
71*/
72
73/*!
74 \qmlproperty ControlStateStyle ControlStateStyle::hovered
75
76 Style overrides applied when the control is \l {StyleReader::}{hovered}.
77
78 \sa pressed, highlighted, focused, checked, vertical, disabled,
79 {StyleReader::hovered}{StyleReader.hovered}
80*/
81
82/*!
83 \qmlproperty ControlStateStyle ControlStateStyle::highlighted
84
85 Style overrides applied when the control is \l {StyleReader::}{highlighted}.
86
87 \sa pressed, hovered, focused, checked, vertical, disabled,
88 {StyleReader::highlighted}{StyleReader.highlighted}
89*/
90
91/*!
92 \qmlproperty ControlStateStyle ControlStateStyle::focused
93
94 Style overrides applied when the control is \l {StyleReader::}{focused}.
95
96 \sa pressed, hovered, highlighted, checked, vertical, disabled,
97 {StyleReader::focused}{StyleReader.focused}
98*/
99
100/*!
101 \qmlproperty ControlStateStyle ControlStateStyle::checked
102
103 Style overrides applied when the control is \l {StyleReader::}{checked}.
104
105 \sa pressed, hovered, highlighted, focused, vertical, disabled,
106 {StyleReader::checked}{StyleReader.checked}
107*/
108
109/*!
110 \qmlproperty ControlStateStyle ControlStateStyle::vertical
111
112 Style overrides applied when the control is \l {StyleReader::}{vertical}
113 (for example, a vertical \l Slider or \l ScrollBar).
114
115 \sa pressed, hovered, highlighted, focused, checked, disabled,
116 {StyleReader::vertical}{StyleReader.vertical}
117*/
118
119/*!
120 \qmlproperty ControlStateStyle ControlStateStyle::disabled
121
122 Style overrides applied when the control is \l {StyleReader::}{disabled}.
123
124 A disabled control cannot be interacted with, so \l pressed,
125 \l hovered, \l highlighted, and \l focused will not be applied
126 at the same time as disabled.
127
128 \sa pressed, hovered, highlighted, focused, checked, vertical,
129 {StyleReader::enabled}{StyleReader.enabled}
130*/
131
133 : QQStyleKitControlProperties(QQSK::PropertyGroup::Control, parent)
134{
135}
136
138{
139 if (m_nestedState == QQSK::StateFlag::Normal) {
140 Q_ASSERT(qobject_cast<const QQStyleKitControl *>(this));
141 auto *self = const_cast<QQStyleKitControlState *>(this);
142 return static_cast<QQStyleKitControl *>(self);
143 }
144 Q_ASSERT(qobject_cast<const QQStyleKitControl *>(parent()));
145 return static_cast<QQStyleKitControl *>(parent());
146}
147
148QQStyleKitControlState *QQStyleKitControlState::lazyCreateState(QQSK::StateFlag state) const
149{
150 if (m_nestedStateObjects.contains(state))
151 return m_nestedStateObjects.value(state);
152
153 QQStyleKitControlState *stateObj = new QQStyleKitControlState(control());
154 stateObj->m_nestedState = m_nestedState;
155 stateObj->m_nestedState.setFlag(state);
156 stateObj->m_nestedState.setFlag(QQSK::StateFlag::Normal, false);
157
158 auto *self = const_cast<QQStyleKitControlState *>(this);
159 self->m_nestedStateObjects.insert(state, stateObj);
160
161 return stateObj;
162}
163
165{
166 return lazyCreateState(QQSK::StateFlag::Pressed);
167}
168
170{
171 return lazyCreateState(QQSK::StateFlag::Hovered);
172}
173
175{
176 return lazyCreateState(QQSK::StateFlag::Highlighted);
177}
178
180{
181 return lazyCreateState(QQSK::StateFlag::Focused);
182}
183
185{
186 return lazyCreateState(QQSK::StateFlag::Checked);
187}
188
190{
191 return lazyCreateState(QQSK::StateFlag::Vertical);
192}
193
195{
196 return lazyCreateState(QQSK::StateFlag::Disabled);
197}
198
199QT_END_NAMESPACE
200
201#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.