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
qquickgroupbox.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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// Qt-Security score:significant reason:default
4
8
9#include <QtGui/qpa/qplatformtheme.h>
10
12
13/*!
14 \qmltype GroupBox
15 \inherits Frame
16//! \nativetype QQuickGroupBox
17 \inqmlmodule QtQuick.Controls
18 \since 5.7
19 \ingroup qtquickcontrols-containers
20 \brief Visual frame and title for a logical group of controls.
21
22 GroupBox is used to layout a logical group of controls together, within
23 a \l {title}{titled} visual frame. GroupBox does not provide a layout of its own, but
24 requires you to position its contents, for instance by creating a \l RowLayout
25 or a \l ColumnLayout.
26
27 Items declared as children of a GroupBox are automatically parented to the
28 GroupBox's \l {Control::}{contentItem}. Items created dynamically need to be
29 explicitly parented to the contentItem.
30
31 If only a single item is used within a GroupBox, it will resize to fit the
32 implicit size of its contained item. This makes it particularly suitable
33 for use together with layouts.
34
35 \image qtquickcontrols-groupbox.png
36
37 \snippet qtquickcontrols-groupbox.qml 1
38
39 \section2 Checkable GroupBox
40
41 Even though GroupBox has no built-in check box, it is straightforward
42 to create a checkable GroupBox by pairing it with a CheckBox.
43
44 \image qtquickcontrols-groupbox-checkable.png
45
46 It is a common pattern to enable or disable the groupbox's children when
47 its checkbox is toggled on or off, but it is up to the application to decide
48 on the behavior of the checkbox.
49
50 \snippet qtquickcontrols-groupbox-checkable.qml 1
51
52 \sa CheckBox, {Customizing GroupBox}, {Container Controls}
53*/
54
56{
57 Q_DECLARE_PUBLIC(QQuickGroupBox)
58
59public:
61 void executeLabel(bool complete = false);
62
63 void itemImplicitWidthChanged(QQuickItem *item) override;
64 void itemImplicitHeightChanged(QQuickItem *item) override;
65 void itemDestroyed(QQuickItem *item) override;
66
67 QPalette defaultPalette() const override { return QQuickTheme::palette(QQuickTheme::GroupBox); }
68
69 QString title;
71};
72
73static inline QString labelName() { return QStringLiteral("label"); }
74
75void QQuickGroupBoxPrivate::cancelLabel()
76{
77 Q_Q(QQuickGroupBox);
78 quickCancelDeferred(q, labelName());
79}
80
82{
83 Q_Q(QQuickGroupBox);
84 if (label.wasExecuted())
85 return;
86
87 if (!label || complete)
88 quickBeginDeferred(q, labelName(), label);
89 if (complete)
90 quickCompleteDeferred(q, labelName(), label);
91}
92
94{
95 Q_Q(QQuickGroupBox);
96 QQuickFramePrivate::itemImplicitWidthChanged(item);
97 if (item == label)
98 emit q->implicitLabelWidthChanged();
99}
100
102{
103 Q_Q(QQuickGroupBox);
104 QQuickFramePrivate::itemImplicitHeightChanged(item);
105 if (item == label)
106 emit q->implicitLabelHeightChanged();
107}
108
110{
111 Q_Q(QQuickGroupBox);
112 QQuickFramePrivate::itemDestroyed(item);
113 if (item == label) {
114 label = nullptr;
115 emit q->labelChanged();
116 }
117}
118
119QQuickGroupBox::QQuickGroupBox(QQuickItem *parent)
120 : QQuickFrame(*(new QQuickGroupBoxPrivate), parent)
121{
122}
123
124QQuickGroupBox::~QQuickGroupBox()
125{
126 Q_D(QQuickGroupBox);
127 d->removeImplicitSizeListener(d->label);
128}
129
130/*!
131 \qmlproperty string QtQuick.Controls::GroupBox::title
132
133 This property holds the title.
134
135 The title is typically displayed above the groupbox to
136 summarize its contents.
137*/
138QString QQuickGroupBox::title() const
139{
140 Q_D(const QQuickGroupBox);
141 return d->title;
142}
143
144void QQuickGroupBox::setTitle(const QString &title)
145{
146 Q_D(QQuickGroupBox);
147 if (d->title == title)
148 return;
149
150 d->title = title;
151 maybeSetAccessibleName(title);
152 emit titleChanged();
153}
154
155/*!
156 \qmlproperty Item QtQuick.Controls::GroupBox::label
157
158 This property holds the label item that visualizes \l title.
159
160 \sa {Customizing GroupBox}
161*/
162QQuickItem *QQuickGroupBox::label() const
163{
164 QQuickGroupBoxPrivate *d = const_cast<QQuickGroupBoxPrivate *>(d_func());
165 if (!d->label)
166 d->executeLabel();
167 return d->label;
168}
169
170void QQuickGroupBox::setLabel(QQuickItem *label)
171{
172 Q_D(QQuickGroupBox);
173 if (d->label == label)
174 return;
175
176 QQuickControlPrivate::warnIfCustomizationNotSupported(this, label, QStringLiteral("label"));
177
178 if (!d->label.isExecuting())
179 d->cancelLabel();
180
181 const qreal oldImplicitLabelWidth = implicitLabelWidth();
182 const qreal oldImplicitLabelHeight = implicitLabelHeight();
183
184 d->removeImplicitSizeListener(d->label);
185 QQuickControlPrivate::hideOldItem(d->label);
186 d->label = label;
187
188 if (label) {
189 if (!label->parentItem())
190 label->setParentItem(this);
191 d->addImplicitSizeListener(label);
192 }
193
194 if (!qFuzzyCompare(oldImplicitLabelWidth, implicitLabelWidth()))
195 emit implicitLabelWidthChanged();
196 if (!qFuzzyCompare(oldImplicitLabelHeight, implicitLabelHeight()))
197 emit implicitLabelHeightChanged();
198 if (!d->label.isExecuting())
199 emit labelChanged();
200}
201
202/*!
203 \since QtQuick.Controls 2.5 (Qt 5.12)
204 \qmlproperty real QtQuick.Controls::GroupBox::implicitLabelWidth
205 \readonly
206
207 This property holds the implicit label width.
208
209 The value is equal to \c {label ? label.implicitWidth : 0}.
210
211 \sa implicitLabelHeight
212*/
213qreal QQuickGroupBox::implicitLabelWidth() const
214{
215 Q_D(const QQuickGroupBox);
216 if (!d->label)
217 return 0;
218 return d->label->implicitWidth();
219}
220
221/*!
222 \since QtQuick.Controls 2.5 (Qt 5.12)
223 \qmlproperty real QtQuick.Controls::GroupBox::implicitLabelHeight
224 \readonly
225
226 This property holds the implicit label height.
227
228 The value is equal to \c {label ? label.implicitHeight : 0}.
229
230 \sa implicitLabelWidth
231*/
232qreal QQuickGroupBox::implicitLabelHeight() const
233{
234 Q_D(const QQuickGroupBox);
235 if (!d->label)
236 return 0;
237 return d->label->implicitHeight();
238}
239
240void QQuickGroupBox::componentComplete()
241{
242 Q_D(QQuickGroupBox);
243 d->executeLabel(true);
244 QQuickFrame::componentComplete();
245}
246
247QFont QQuickGroupBox::defaultFont() const
248{
249 return QQuickTheme::font(QQuickTheme::GroupBox);
250}
251
252#if QT_CONFIG(accessibility)
253QAccessible::Role QQuickGroupBox::accessibleRole() const
254{
255 return QAccessible::Grouping;
256}
257
258void QQuickGroupBox::accessibilityActiveChanged(bool active)
259{
260 Q_D(QQuickGroupBox);
261 QQuickFrame::accessibilityActiveChanged(active);
262
263 if (active)
264 maybeSetAccessibleName(d->title);
265}
266#endif
267
268QT_END_NAMESPACE
269
270#include "moc_qquickgroupbox_p.cpp"
Visual frame and title for a logical group of controls.
QQuickDeferredPointer< QQuickItem > label
void executeLabel(bool complete=false)
void itemDestroyed(QQuickItem *item) override
void itemImplicitWidthChanged(QQuickItem *item) override
void itemImplicitHeightChanged(QQuickItem *item) override
QPalette defaultPalette() const override
static QString labelName()