Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
5
#
include
"qquickgroupbox_p.h"
6
#
include
"qquickframe_p_p.h"
7
#
include
"qquickdeferredexecute_p_p.h"
8
#
include
"qquicktemplatesutils_p.h"
9
10
#
include
<
QtGui
/
qtextdocument
.
h
>
11
12
#
include
<
QtGui
/
qpa
/
qplatformtheme
.
h
>
13
14
QT_BEGIN_NAMESPACE
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
60
class
QQuickGroupBoxPrivate
:
public
QQuickFramePrivate
61
{
62
Q_DECLARE_PUBLIC(QQuickGroupBox)
63
64
public
:
65
void
cancelLabel
();
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
74
bool
needsTitleFormatWarning
()
const
75
{
76
return
!titleFormat && !titleFormatWarningDisplayed && Qt::mightBeRichText(title);
77
}
78
79
QString
title
;
80
QQuickDeferredPointer
<
QQuickItem
>
label
;
81
std
::
optional
<
QQuickText
::
TextFormat
>
titleFormat
;
82
bool
titleFormatWarningDisplayed
=
false
;
83
};
84
85
static
inline
QString
labelName
() {
return
QStringLiteral(
"label"
); }
86
87
void
QQuickGroupBoxPrivate
::cancelLabel()
88
{
89
Q_Q(QQuickGroupBox);
90
quickCancelDeferred(q, labelName());
91
}
92
93
void
QQuickGroupBoxPrivate
::
executeLabel
(
bool
complete)
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
105
void
QQuickGroupBoxPrivate
::
itemImplicitWidthChanged
(QQuickItem *item)
106
{
107
Q_Q(QQuickGroupBox);
108
QQuickFramePrivate::itemImplicitWidthChanged(item);
109
if
(item == label)
110
emit q->implicitLabelWidthChanged();
111
}
112
113
void
QQuickGroupBoxPrivate
::
itemImplicitHeightChanged
(QQuickItem *item)
114
{
115
Q_Q(QQuickGroupBox);
116
QQuickFramePrivate::itemImplicitHeightChanged(item);
117
if
(item == label)
118
emit q->implicitLabelHeightChanged();
119
}
120
121
void
QQuickGroupBoxPrivate
::
itemDestroyed
(QQuickItem *item)
122
{
123
Q_Q(QQuickGroupBox);
124
QQuickFramePrivate::itemDestroyed(item);
125
if
(item == label) {
126
label =
nullptr
;
127
emit q->labelChanged();
128
}
129
}
130
131
QQuickGroupBox::QQuickGroupBox(QQuickItem *parent)
132
: QQuickFrame(*(
new
QQuickGroupBoxPrivate), parent)
133
{
134
}
135
136
QQuickGroupBox::~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
*/
150
QString QQuickGroupBox::title()
const
151
{
152
Q_D(
const
QQuickGroupBox);
153
return
d->title;
154
}
155
156
void
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
*/
176
QQuickItem *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
184
void
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
*/
227
qreal 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
*/
246
qreal QQuickGroupBox::implicitLabelHeight()
const
247
{
248
Q_D(
const
QQuickGroupBox);
249
if
(!d->label)
250
return
0;
251
return
d->label->implicitHeight();
252
}
253
254
void
QQuickGroupBox::componentComplete()
255
{
256
Q_D(QQuickGroupBox);
257
d->executeLabel(
true
);
258
QQuickFrame::componentComplete();
259
if
(d->needsTitleFormatWarning())
260
polish();
261
}
262
263
void
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
*/
285
QQuickText::TextFormat QQuickGroupBox::titleFormat()
const
286
{
287
Q_D(
const
QQuickGroupBox);
288
return
d->titleFormat.value_or(QQuickText::AutoText);
289
}
290
291
void
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
300
void
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
311
QFont QQuickGroupBox::defaultFont()
const
312
{
313
return
QQuickTheme::font(QQuickTheme::GroupBox);
314
}
315
316
#
if
QT_CONFIG
(
accessibility
)
317
QAccessible::Role QQuickGroupBox::accessibleRole()
const
318
{
319
return
QAccessible::Grouping;
320
}
321
322
void
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
332
QT_END_NAMESPACE
333
334
#
include
"moc_qquickgroupbox_p.cpp"
QQuickGroupBoxPrivate
Visual frame and title for a logical group of controls.
Definition
qquickgroupbox.cpp:61
QQuickGroupBoxPrivate::label
QQuickDeferredPointer< QQuickItem > label
Definition
qquickgroupbox.cpp:80
QQuickGroupBoxPrivate::title
QString title
Definition
qquickgroupbox.cpp:79
QQuickGroupBoxPrivate::executeLabel
void executeLabel(bool complete=false)
Definition
qquickgroupbox.cpp:93
QQuickGroupBoxPrivate::titleFormat
std::optional< QQuickText::TextFormat > titleFormat
Definition
qquickgroupbox.cpp:81
QQuickGroupBoxPrivate::itemDestroyed
void itemDestroyed(QQuickItem *item) override
Definition
qquickgroupbox.cpp:121
QQuickGroupBoxPrivate::itemImplicitWidthChanged
void itemImplicitWidthChanged(QQuickItem *item) override
Definition
qquickgroupbox.cpp:105
QQuickGroupBoxPrivate::titleFormatWarningDisplayed
bool titleFormatWarningDisplayed
Definition
qquickgroupbox.cpp:82
QQuickGroupBoxPrivate::itemImplicitHeightChanged
void itemImplicitHeightChanged(QQuickItem *item) override
Definition
qquickgroupbox.cpp:113
QQuickGroupBoxPrivate::defaultPalette
QPalette defaultPalette() const override
Definition
qquickgroupbox.cpp:72
QQuickGroupBoxPrivate::needsTitleFormatWarning
bool needsTitleFormatWarning() const
Definition
qquickgroupbox.cpp:74
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
labelName
static QString labelName()
Definition
qquickgroupbox.cpp:85
qtdeclarative
src
quicktemplates
qquickgroupbox.cpp
Generated on
for Qt by
1.18.0