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
9
10#include <QtGui/qtextdocument.h>
11
12#include <QtGui/qpa/qplatformtheme.h>
13
15
16/*!
17 \qmltype GroupBox
18 \inherits Frame
19//! \nativetype QQuickGroupBox
20 \inqmlmodule QtQuick.Controls
21 \since 5.7
22 \ingroup qtquickcontrols-containers
23 \brief Visual frame and title for a logical group of controls.
24
25 GroupBox is used to layout a logical group of controls together, within
26 a \l {title}{titled} visual frame. GroupBox does not provide a layout of its own, but
27 requires you to position its contents, for instance by creating a \l RowLayout
28 or a \l ColumnLayout.
29
30 Items declared as children of a GroupBox are automatically parented to the
31 GroupBox's \l {Control::}{contentItem}. Items created dynamically need to be
32 explicitly parented to the contentItem.
33
34 If only a single item is used within a GroupBox, it will resize to fit the
35 implicit size of its contained item. This makes it particularly suitable
36 for use together with layouts.
37
38 \image qtquickcontrols-groupbox.png
39 {Group box with title and border containing checkboxes}
40
41 \snippet qtquickcontrols-groupbox.qml 1
42
43 \section2 Checkable GroupBox
44
45 Even though GroupBox has no built-in check box, it is straightforward
46 to create a checkable GroupBox by pairing it with a CheckBox.
47
48 \image qtquickcontrols-groupbox-checkable.png
49 {Checkable group box with checkbox in title}
50
51 It is a common pattern to enable or disable the groupbox's children when
52 its checkbox is toggled on or off, but it is up to the application to decide
53 on the behavior of the checkbox.
54
55 \snippet qtquickcontrols-groupbox-checkable.qml 1
56
57 \sa CheckBox, {Customizing GroupBox}, {Container Controls}
58*/
59
61{
62 Q_DECLARE_PUBLIC(QQuickGroupBox)
63
64public:
66 void executeLabel(bool complete = false);
67
68 void itemImplicitWidthChanged(QQuickItem *item) override;
69 void itemImplicitHeightChanged(QQuickItem *item) override;
70 void itemDestroyed(QQuickItem *item) override;
71
72 QPalette defaultPalette() const override { return QQuickTheme::palette(QQuickTheme::GroupBox); }
73
75 {
76 return !titleFormat && !titleFormatWarningDisplayed && Qt::mightBeRichText(title);
77 }
78
79 QString title;
83};
84
85static inline QString labelName() { return QStringLiteral("label"); }
86
87void QQuickGroupBoxPrivate::cancelLabel()
88{
89 Q_Q(QQuickGroupBox);
90 quickCancelDeferred(q, labelName());
91}
92
94{
95 Q_Q(QQuickGroupBox);
96 if (label.wasExecuted())
97 return;
98
99 if (!label || complete)
100 quickBeginDeferred(q, labelName(), label);
101 if (complete)
102 quickCompleteDeferred(q, labelName(), label);
103}
104
106{
107 Q_Q(QQuickGroupBox);
108 QQuickFramePrivate::itemImplicitWidthChanged(item);
109 if (item == label)
110 emit q->implicitLabelWidthChanged();
111}
112
114{
115 Q_Q(QQuickGroupBox);
116 QQuickFramePrivate::itemImplicitHeightChanged(item);
117 if (item == label)
118 emit q->implicitLabelHeightChanged();
119}
120
122{
123 Q_Q(QQuickGroupBox);
124 QQuickFramePrivate::itemDestroyed(item);
125 if (item == label) {
126 label = nullptr;
127 emit q->labelChanged();
128 }
129}
130
131QQuickGroupBox::QQuickGroupBox(QQuickItem *parent)
132 : QQuickFrame(*(new QQuickGroupBoxPrivate), parent)
133{
134}
135
136QQuickGroupBox::~QQuickGroupBox()
137{
138 Q_D(QQuickGroupBox);
139 d->removeImplicitSizeListener(d->label);
140}
141
142/*!
143 \qmlproperty string QtQuick.Controls::GroupBox::title
144
145 This property holds the title.
146
147 The title is typically displayed above the groupbox to
148 summarize its contents.
149*/
150QString QQuickGroupBox::title() const
151{
152 Q_D(const QQuickGroupBox);
153 return d->title;
154}
155
156void QQuickGroupBox::setTitle(const QString &title)
157{
158 Q_D(QQuickGroupBox);
159 if (d->title == title)
160 return;
161
162 d->title = title;
163 maybeSetAccessibleName(title);
164 if (isComponentComplete() && d->needsTitleFormatWarning())
165 polish();
166 emit titleChanged();
167}
168
169/*!
170 \qmlproperty Item QtQuick.Controls::GroupBox::label
171
172 This property holds the label item that visualizes \l title.
173
174 \sa {Customizing GroupBox}
175*/
176QQuickItem *QQuickGroupBox::label() const
177{
178 QQuickGroupBoxPrivate *d = const_cast<QQuickGroupBoxPrivate *>(d_func());
179 if (!d->label)
180 d->executeLabel();
181 return d->label;
182}
183
184void QQuickGroupBox::setLabel(QQuickItem *label)
185{
186 Q_D(QQuickGroupBox);
187 if (d->label == label)
188 return;
189
190 QQuickControlPrivate::warnIfCustomizationNotSupported(this, label, QStringLiteral("label"));
191
192 if (!d->label.isExecuting())
193 d->cancelLabel();
194
195 const qreal oldImplicitLabelWidth = implicitLabelWidth();
196 const qreal oldImplicitLabelHeight = implicitLabelHeight();
197
198 d->removeImplicitSizeListener(d->label);
199 QQuickControlPrivate::hideOldItem(d->label);
200 d->label = label;
201
202 if (label) {
203 if (!label->parentItem())
204 label->setParentItem(this);
205 d->addImplicitSizeListener(label);
206 }
207
208 if (!qFuzzyCompare(oldImplicitLabelWidth, implicitLabelWidth()))
209 emit implicitLabelWidthChanged();
210 if (!qFuzzyCompare(oldImplicitLabelHeight, implicitLabelHeight()))
211 emit implicitLabelHeightChanged();
212 if (!d->label.isExecuting())
213 emit labelChanged();
214}
215
216/*!
217 \since QtQuick.Controls 2.5 (Qt 5.12)
218 \qmlproperty real QtQuick.Controls::GroupBox::implicitLabelWidth
219 \readonly
220
221 This property holds the implicit label width.
222
223 The value is equal to \c {label ? label.implicitWidth : 0}.
224
225 \sa implicitLabelHeight
226*/
227qreal QQuickGroupBox::implicitLabelWidth() const
228{
229 Q_D(const QQuickGroupBox);
230 if (!d->label)
231 return 0;
232 return d->label->implicitWidth();
233}
234
235/*!
236 \since QtQuick.Controls 2.5 (Qt 5.12)
237 \qmlproperty real QtQuick.Controls::GroupBox::implicitLabelHeight
238 \readonly
239
240 This property holds the implicit label height.
241
242 The value is equal to \c {label ? label.implicitHeight : 0}.
243
244 \sa implicitLabelWidth
245*/
246qreal QQuickGroupBox::implicitLabelHeight() const
247{
248 Q_D(const QQuickGroupBox);
249 if (!d->label)
250 return 0;
251 return d->label->implicitHeight();
252}
253
254void QQuickGroupBox::componentComplete()
255{
256 Q_D(QQuickGroupBox);
257 d->executeLabel(true);
258 QQuickFrame::componentComplete();
259 if (d->needsTitleFormatWarning())
260 polish();
261}
262
263void QQuickGroupBox::updatePolish()
264{
265 Q_D(QQuickGroupBox);
266 QQuickFrame::updatePolish();
267 if (d->needsTitleFormatWarning()) {
268 QQuickTemplatesUtils::warnAboutImplicitRichText(this);
269 d->titleFormatWarningDisplayed = true;
270 }
271}
272
273/*!
274 \since Qt 6.13
275 \qmlproperty enumeration QtQuick.Controls::GroupBox::titleFormat
276
277 This property holds the format of the \l title.
278
279 The supported values are the same as for \l {Text::textFormat}{Text.textFormat}.
280 The default value is \c Text.AutoText.
281
282 \note This property is applied by the group box's \l label.
283 A custom label needs to take it into account itself.
284*/
285QQuickText::TextFormat QQuickGroupBox::titleFormat() const
286{
287 Q_D(const QQuickGroupBox);
288 return d->titleFormat.value_or(QQuickText::AutoText);
289}
290
291void QQuickGroupBox::setTitleFormat(QQuickText::TextFormat titleFormat)
292{
293 Q_D(QQuickGroupBox);
294 const bool changed = titleFormat != this->titleFormat();
295 d->titleFormat = titleFormat;
296 if (changed)
297 emit titleFormatChanged();
298}
299
300void QQuickGroupBox::resetTitleFormat()
301{
302 Q_D(QQuickGroupBox);
303 const bool changed = titleFormat() != QQuickText::AutoText;
304 d->titleFormat.reset();
305 if (changed)
306 emit titleFormatChanged();
307 if (isComponentComplete() && d->needsTitleFormatWarning())
308 polish();
309}
310
311QFont QQuickGroupBox::defaultFont() const
312{
313 return QQuickTheme::font(QQuickTheme::GroupBox);
314}
315
316#if QT_CONFIG(accessibility)
317QAccessible::Role QQuickGroupBox::accessibleRole() const
318{
319 return QAccessible::Grouping;
320}
321
322void QQuickGroupBox::accessibilityActiveChanged(bool active)
323{
324 Q_D(QQuickGroupBox);
325 QQuickFrame::accessibilityActiveChanged(active);
326
327 if (active)
328 maybeSetAccessibleName(d->title);
329}
330#endif
331
332QT_END_NAMESPACE
333
334#include "moc_qquickgroupbox_p.cpp"
Visual frame and title for a logical group of controls.
QQuickDeferredPointer< QQuickItem > label
void executeLabel(bool complete=false)
std::optional< QQuickText::TextFormat > titleFormat
void itemDestroyed(QQuickItem *item) override
void itemImplicitWidthChanged(QQuickItem *item) override
void itemImplicitHeightChanged(QQuickItem *item) override
QPalette defaultPalette() const override
bool needsTitleFormatWarning() const
Combined button and popup list for selecting options.
static QString labelName()