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 {Group box with title and border containing checkboxes}
37
38 \snippet qtquickcontrols-groupbox.qml 1
39
40 \section2 Checkable GroupBox
41
42 Even though GroupBox has no built-in check box, it is straightforward
43 to create a checkable GroupBox by pairing it with a CheckBox.
44
45 \image qtquickcontrols-groupbox-checkable.png
46 {Checkable group box with checkbox in title}
47
48 It is a common pattern to enable or disable the groupbox's children when
49 its checkbox is toggled on or off, but it is up to the application to decide
50 on the behavior of the checkbox.
51
52 \snippet qtquickcontrols-groupbox-checkable.qml 1
53
54 \sa CheckBox, {Customizing GroupBox}, {Container Controls}
55*/
56
58{
59 Q_DECLARE_PUBLIC(QQuickGroupBox)
60
61public:
63 void executeLabel(bool complete = false);
64
65 void itemImplicitWidthChanged(QQuickItem *item) override;
66 void itemImplicitHeightChanged(QQuickItem *item) override;
67 void itemDestroyed(QQuickItem *item) override;
68
69 QPalette defaultPalette() const override { return QQuickTheme::palette(QQuickTheme::GroupBox); }
70
71 QString title;
73};
74
75static inline QString labelName() { return QStringLiteral("label"); }
76
77void QQuickGroupBoxPrivate::cancelLabel()
78{
79 Q_Q(QQuickGroupBox);
80 quickCancelDeferred(q, labelName());
81}
82
84{
85 Q_Q(QQuickGroupBox);
86 if (label.wasExecuted())
87 return;
88
89 if (!label || complete)
90 quickBeginDeferred(q, labelName(), label);
91 if (complete)
92 quickCompleteDeferred(q, labelName(), label);
93}
94
96{
97 Q_Q(QQuickGroupBox);
98 QQuickFramePrivate::itemImplicitWidthChanged(item);
99 if (item == label)
100 emit q->implicitLabelWidthChanged();
101}
102
104{
105 Q_Q(QQuickGroupBox);
106 QQuickFramePrivate::itemImplicitHeightChanged(item);
107 if (item == label)
108 emit q->implicitLabelHeightChanged();
109}
110
112{
113 Q_Q(QQuickGroupBox);
114 QQuickFramePrivate::itemDestroyed(item);
115 if (item == label) {
116 label = nullptr;
117 emit q->labelChanged();
118 }
119}
120
121QQuickGroupBox::QQuickGroupBox(QQuickItem *parent)
122 : QQuickFrame(*(new QQuickGroupBoxPrivate), parent)
123{
124}
125
126QQuickGroupBox::~QQuickGroupBox()
127{
128 Q_D(QQuickGroupBox);
129 d->removeImplicitSizeListener(d->label);
130}
131
132/*!
133 \qmlproperty string QtQuick.Controls::GroupBox::title
134
135 This property holds the title.
136
137 The title is typically displayed above the groupbox to
138 summarize its contents.
139*/
140QString QQuickGroupBox::title() const
141{
142 Q_D(const QQuickGroupBox);
143 return d->title;
144}
145
146void QQuickGroupBox::setTitle(const QString &title)
147{
148 Q_D(QQuickGroupBox);
149 if (d->title == title)
150 return;
151
152 d->title = title;
153 maybeSetAccessibleName(title);
154 emit titleChanged();
155}
156
157/*!
158 \qmlproperty Item QtQuick.Controls::GroupBox::label
159
160 This property holds the label item that visualizes \l title.
161
162 \sa {Customizing GroupBox}
163*/
164QQuickItem *QQuickGroupBox::label() const
165{
166 QQuickGroupBoxPrivate *d = const_cast<QQuickGroupBoxPrivate *>(d_func());
167 if (!d->label)
168 d->executeLabel();
169 return d->label;
170}
171
172void QQuickGroupBox::setLabel(QQuickItem *label)
173{
174 Q_D(QQuickGroupBox);
175 if (d->label == label)
176 return;
177
178 QQuickControlPrivate::warnIfCustomizationNotSupported(this, label, QStringLiteral("label"));
179
180 if (!d->label.isExecuting())
181 d->cancelLabel();
182
183 const qreal oldImplicitLabelWidth = implicitLabelWidth();
184 const qreal oldImplicitLabelHeight = implicitLabelHeight();
185
186 d->removeImplicitSizeListener(d->label);
187 QQuickControlPrivate::hideOldItem(d->label);
188 d->label = label;
189
190 if (label) {
191 if (!label->parentItem())
192 label->setParentItem(this);
193 d->addImplicitSizeListener(label);
194 }
195
196 if (!qFuzzyCompare(oldImplicitLabelWidth, implicitLabelWidth()))
197 emit implicitLabelWidthChanged();
198 if (!qFuzzyCompare(oldImplicitLabelHeight, implicitLabelHeight()))
199 emit implicitLabelHeightChanged();
200 if (!d->label.isExecuting())
201 emit labelChanged();
202}
203
204/*!
205 \since QtQuick.Controls 2.5 (Qt 5.12)
206 \qmlproperty real QtQuick.Controls::GroupBox::implicitLabelWidth
207 \readonly
208
209 This property holds the implicit label width.
210
211 The value is equal to \c {label ? label.implicitWidth : 0}.
212
213 \sa implicitLabelHeight
214*/
215qreal QQuickGroupBox::implicitLabelWidth() const
216{
217 Q_D(const QQuickGroupBox);
218 if (!d->label)
219 return 0;
220 return d->label->implicitWidth();
221}
222
223/*!
224 \since QtQuick.Controls 2.5 (Qt 5.12)
225 \qmlproperty real QtQuick.Controls::GroupBox::implicitLabelHeight
226 \readonly
227
228 This property holds the implicit label height.
229
230 The value is equal to \c {label ? label.implicitHeight : 0}.
231
232 \sa implicitLabelWidth
233*/
234qreal QQuickGroupBox::implicitLabelHeight() const
235{
236 Q_D(const QQuickGroupBox);
237 if (!d->label)
238 return 0;
239 return d->label->implicitHeight();
240}
241
242void QQuickGroupBox::componentComplete()
243{
244 Q_D(QQuickGroupBox);
245 d->executeLabel(true);
246 QQuickFrame::componentComplete();
247}
248
249QFont QQuickGroupBox::defaultFont() const
250{
251 return QQuickTheme::font(QQuickTheme::GroupBox);
252}
253
254#if QT_CONFIG(accessibility)
255QAccessible::Role QQuickGroupBox::accessibleRole() const
256{
257 return QAccessible::Grouping;
258}
259
260void QQuickGroupBox::accessibilityActiveChanged(bool active)
261{
262 Q_D(QQuickGroupBox);
263 QQuickFrame::accessibilityActiveChanged(active);
264
265 if (active)
266 maybeSetAccessibleName(d->title);
267}
268#endif
269
270QT_END_NAMESPACE
271
272#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
Combined button and popup list for selecting options.
static QString labelName()