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
qsizepolicy.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 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
"qsizepolicy.h"
6
7
#
include
<
qdatastream
.
h
>
8
#
include
<
qdebug
.
h
>
9
#
include
<
qvariant
.
h
>
10
11
QT_BEGIN_NAMESPACE
12
13
/*!
14
\class QSizePolicy
15
\brief The QSizePolicy class is a layout attribute describing horizontal
16
and vertical resizing policy.
17
18
\ingroup geomanagement
19
\inmodule QtWidgets
20
21
The size policy of a widget is an expression of its willingness to
22
be resized in various ways, and affects how the widget is treated
23
by the \l{Layout Management}{layout engine}. Each widget returns a
24
QSizePolicy that describes the horizontal and vertical resizing
25
policy it prefers when being laid out. You can change this for
26
a specific widget by changing its QWidget::sizePolicy property.
27
28
QSizePolicy contains two independent QSizePolicy::Policy values
29
and two stretch factors; one describes the widgets's horizontal
30
size policy, and the other describes its vertical size policy. It
31
also contains a flag to indicate whether the height and width of
32
its preferred size are related.
33
34
The horizontal and vertical policies can be set in the
35
constructor, and altered using the setHorizontalPolicy() and
36
setVerticalPolicy() functions. The stretch factors can be set
37
using the setHorizontalStretch() and setVerticalStretch()
38
functions. The flag indicating whether the widget's
39
\l{QWidget::sizeHint()}{sizeHint()} is width-dependent (such as a
40
menu bar or a word-wrapping label) can be set using the
41
setHeightForWidth() function.
42
43
The current size policies and stretch factors be retrieved using
44
the horizontalPolicy(), verticalPolicy(), horizontalStretch() and
45
verticalStretch() functions. Alternatively, use the transpose()
46
function to swap the horizontal and vertical policies and
47
stretches. The hasHeightForWidth() function returns the current
48
status of the flag indicating the size hint dependencies.
49
50
Use the expandingDirections() function to determine whether the
51
associated widget can make use of more space than its
52
\l{QWidget::sizeHint()}{sizeHint()} function indicates, as well as
53
find out in which directions it can expand.
54
55
Finally, the QSizePolicy class provides operators comparing this
56
size policy to a given policy, as well as a QVariant operator
57
storing this QSizePolicy as a QVariant object.
58
59
\sa QSize, QWidget::sizeHint(), QWidget::sizePolicy,
60
QLayoutItem::sizeHint()
61
*/
62
63
/*!
64
\enum QSizePolicy::PolicyFlag
65
66
These flags are combined together to form the various \l{Policy}
67
values:
68
69
\value GrowFlag The widget can grow beyond its size hint if necessary.
70
\value ExpandFlag The widget should get as much space as possible.
71
\value ShrinkFlag The widget can shrink below its size hint if necessary.
72
\value IgnoreFlag The widget's size hint is ignored. The widget will get
73
as much space as possible.
74
75
\sa Policy
76
*/
77
78
/*!
79
\enum QSizePolicy::Policy
80
81
This enum describes the various per-dimension sizing types used
82
when constructing a QSizePolicy.
83
84
\value Fixed The QWidget::sizeHint() is the only acceptable
85
alternative, so the widget can never grow or shrink (e.g. the
86
vertical direction of a push button).
87
88
\value Minimum The sizeHint() is minimal, and sufficient. The
89
widget can be expanded, but there is no advantage to it being
90
larger (e.g. the horizontal direction of a push button).
91
It cannot be smaller than the size provided by sizeHint().
92
93
\value Maximum The sizeHint() is a maximum. The widget can be
94
shrunk any amount without detriment if other widgets need the
95
space (e.g. a separator line).
96
It cannot be larger than the size provided by sizeHint().
97
98
\value Preferred The sizeHint() is best, but the widget can be
99
shrunk and still be useful. The widget can be expanded, but there
100
is no advantage to it being larger than sizeHint() (the default
101
QWidget policy).
102
103
\value Expanding The sizeHint() is a sensible size, but the
104
widget can be shrunk and still be useful. The widget can make use
105
of extra space, so it should get as much space as possible (e.g.
106
the horizontal direction of a horizontal slider).
107
108
\value MinimumExpanding The sizeHint() is minimal, and sufficient.
109
The widget can make use of extra space, so it should get as much
110
space as possible (e.g. the horizontal direction of a horizontal
111
slider).
112
113
\value Ignored The sizeHint() is ignored. The widget will get as
114
much space as possible.
115
116
\sa PolicyFlag, setHorizontalPolicy(), setVerticalPolicy()
117
*/
118
119
/*!
120
\fn QSizePolicy::QSizePolicy()
121
122
Constructs a QSizePolicy object with \l Fixed as its horizontal
123
and vertical policies.
124
125
The policies can be altered using the setHorizontalPolicy() and
126
setVerticalPolicy() functions. Use the setHeightForWidth()
127
function if the preferred height of the widget is dependent on the
128
width of the widget (for example, a QLabel with line wrapping).
129
130
\sa setHorizontalStretch(), setVerticalStretch()
131
*/
132
133
/*!
134
\fn QSizePolicy::QSizePolicy(Policy horizontal, Policy vertical, ControlType type)
135
\since 4.3
136
137
Constructs a QSizePolicy object with the given \a horizontal and
138
\a vertical policies, and the specified control \a type.
139
140
Use setHeightForWidth() if the preferred height of the widget is
141
dependent on the width of the widget (for example, a QLabel with
142
line wrapping).
143
144
\sa setHorizontalStretch(), setVerticalStretch(), controlType()
145
*/
146
147
/*!
148
\fn QSizePolicy::Policy QSizePolicy::horizontalPolicy() const
149
150
Returns the horizontal component of the size policy.
151
152
\sa setHorizontalPolicy(), verticalPolicy(), horizontalStretch()
153
*/
154
155
/*!
156
\fn QSizePolicy::Policy QSizePolicy::verticalPolicy() const
157
158
Returns the vertical component of the size policy.
159
160
\sa setVerticalPolicy(), horizontalPolicy(), verticalStretch()
161
*/
162
163
/*!
164
\fn void QSizePolicy::setHorizontalPolicy(Policy policy)
165
166
Sets the horizontal component to the given \a policy.
167
168
\sa horizontalPolicy(), setVerticalPolicy(), setHorizontalStretch()
169
*/
170
171
/*!
172
\fn void QSizePolicy::setVerticalPolicy(Policy policy)
173
174
Sets the vertical component to the given \a policy.
175
176
\sa verticalPolicy(), setHorizontalPolicy(), setVerticalStretch()
177
*/
178
179
/*!
180
\fn Qt::Orientations QSizePolicy::expandingDirections() const
181
182
Returns whether a widget can make use of more space than the
183
QWidget::sizeHint() function indicates.
184
185
A value of Qt::Horizontal or Qt::Vertical means that the widget
186
can grow horizontally or vertically (i.e., the horizontal or
187
vertical policy is \l Expanding or \l MinimumExpanding), whereas
188
Qt::Horizontal | Qt::Vertical means that it can grow in both
189
dimensions.
190
191
\sa horizontalPolicy(), verticalPolicy()
192
*/
193
194
/*!
195
\since 4.3
196
197
Returns the control type associated with the widget for which
198
this size policy applies.
199
*/
200
QSizePolicy::ControlType QSizePolicy::controlType()
const
noexcept
201
{
202
return
QSizePolicy::ControlType(1 << bits.ctype);
203
}
204
205
206
/*!
207
\since 4.3
208
209
Sets the control type associated with the widget for which this
210
size policy applies to \a type.
211
212
The control type specifies the type of the widget for which this
213
size policy applies. It is used by some styles, notably
214
QMacStyle, to insert proper spacing between widgets. For example,
215
the \macos Aqua guidelines specify that push buttons should be
216
separated by 12 pixels, whereas vertically stacked radio buttons
217
only require 6 pixels.
218
219
\sa QStyle::layoutSpacing()
220
*/
221
void
QSizePolicy::setControlType(ControlType type)
noexcept
222
{
223
bits.ctype = toControlTypeFieldValue(type);
224
}
225
226
/*!
227
\fn void QSizePolicy::setHeightForWidth(bool dependent)
228
229
Sets the flag determining whether the widget's preferred height
230
depends on its width, to \a dependent.
231
232
\sa hasHeightForWidth(), setWidthForHeight()
233
*/
234
235
/*!
236
\fn bool QSizePolicy::hasHeightForWidth() const
237
238
Returns \c true if the widget's preferred height depends on its
239
width; otherwise returns \c false.
240
241
\sa setHeightForWidth()
242
*/
243
244
/*!
245
\fn void QSizePolicy::setWidthForHeight(bool dependent)
246
247
Sets the flag determining whether the widget's width
248
depends on its height, to \a dependent.
249
250
This is only supported for QGraphicsLayout's subclasses.
251
It is not possible to have a layout with both height-for-width
252
and width-for-height constraints at the same time.
253
254
\sa hasWidthForHeight(), setHeightForWidth()
255
*/
256
257
/*!
258
\fn bool QSizePolicy::hasWidthForHeight() const
259
260
Returns \c true if the widget's width depends on its
261
height; otherwise returns \c false.
262
263
\sa setWidthForHeight()
264
*/
265
266
/*!
267
\fn bool QSizePolicy::operator==(const QSizePolicy &other) const
268
269
Returns \c true if this policy is equal to \a other; otherwise
270
returns \c false.
271
272
\sa operator!=()
273
*/
274
275
/*!
276
\fn bool QSizePolicy::operator!=(const QSizePolicy &other) const
277
278
Returns \c true if this policy is different from \a other; otherwise
279
returns \c false.
280
281
\sa operator==()
282
*/
283
284
/*!
285
\fn size_t QSizePolicy::qHash(QSizePolicy key, size_t seed = 0)
286
\since 5.6
287
\qhash{QSizePolicy}
288
*/
289
290
/*!
291
\fn int QSizePolicy::horizontalStretch() const
292
293
Returns the horizontal stretch factor of the size policy.
294
295
\sa setHorizontalStretch(), verticalStretch(), horizontalPolicy()
296
*/
297
298
/*!
299
\fn int QSizePolicy::verticalStretch() const
300
301
Returns the vertical stretch factor of the size policy.
302
303
\sa setVerticalStretch(), horizontalStretch(), verticalPolicy()
304
*/
305
306
/*!
307
\fn void QSizePolicy::setHorizontalStretch(int stretchFactor)
308
309
Sets the horizontal stretch factor of the size policy to the given \a
310
stretchFactor. \a stretchFactor must be in the range [0,255].
311
312
When two widgets are adjacent to each other in a horizontal layout,
313
setting the horizontal stretch factor of the widget on the left to 2
314
and the factor of widget on the right to 1 will ensure that the widget
315
on the left will always be twice the size of the one on the right.
316
317
\sa horizontalStretch(), setVerticalStretch(), setHorizontalPolicy()
318
*/
319
320
/*!
321
\fn void QSizePolicy::setVerticalStretch(int stretchFactor)
322
323
Sets the vertical stretch factor of the size policy to the given
324
\a stretchFactor. \a stretchFactor must be in the range [0,255].
325
326
When two widgets are adjacent to each other in a vertical layout,
327
setting the vertical stretch factor of the widget on the top to 2
328
and the factor of widget on the bottom to 1 will ensure that
329
the widget on the top will always be twice the size of the one
330
on the bottom.
331
332
\sa verticalStretch(), setHorizontalStretch(), setVerticalPolicy()
333
*/
334
335
/*!
336
\fn void QSizePolicy::transpose()
337
338
Swaps the horizontal and vertical policies and stretches.
339
340
\sa transposed()
341
*/
342
343
/*!
344
\fn QSizePolicy QSizePolicy::transposed() const
345
\since 5.9
346
347
Returns a size policy object with the horizontal and vertical
348
policies and stretches swapped.
349
350
\sa transpose()
351
*/
352
353
/*!
354
\fn void QSizePolicy::retainSizeWhenHidden() const
355
\since 5.2
356
357
Returns whether the layout should retain the widget's size when it is hidden.
358
This is \c false by default.
359
360
\sa setRetainSizeWhenHidden()
361
*/
362
363
/*!
364
\fn void QSizePolicy::setRetainSizeWhenHidden(bool retainSize)
365
\since 5.2
366
367
Sets whether a layout should retain the widget's size when it is hidden.
368
If \a retainSize is \c true, the layout will not be changed by hiding the widget.
369
370
\sa retainSizeWhenHidden()
371
*/
372
373
/*!
374
\enum QSizePolicy::ControlType
375
\since 4.3
376
377
This enum specifies the different types of widgets in terms of
378
layout interaction:
379
380
\value DefaultType The default type, when none is specified.
381
\value ButtonBox A QDialogButtonBox instance.
382
\value CheckBox A QCheckBox instance.
383
\value ComboBox A QComboBox instance.
384
\value Frame A QFrame instance.
385
\value GroupBox A QGroupBox instance.
386
\value Label A QLabel instance.
387
\value Line A QFrame instance with QFrame::HLine or QFrame::VLine.
388
\value LineEdit A QLineEdit instance.
389
\value PushButton A QPushButton instance.
390
\value RadioButton A QRadioButton instance.
391
\value Slider A QAbstractSlider instance.
392
\value SpinBox A QAbstractSpinBox instance.
393
\value TabWidget A QTabWidget instance.
394
\value ToolButton A QToolButton instance.
395
396
\sa setControlType(), controlType()
397
*/
398
399
/*!
400
Returns a QVariant storing this QSizePolicy.
401
*/
402
QSizePolicy::operator QVariant()
const
403
{
404
return
QVariant::fromValue(*
this
);
405
}
406
407
#
ifndef
QT_NO_DATASTREAM
408
409
/*!
410
\relates QSizePolicy
411
\since 4.2
412
413
Writes the size \a policy to the data stream \a stream.
414
415
\sa{Serializing Qt Data Types}{Format of the QDataStream operators}
416
*/
417
QDataStream &operator<<(QDataStream &stream,
const
QSizePolicy &policy)
418
{
419
// The order here is for historical reasons. (compatibility with Qt4)
420
quint32 data = (policy.bits.horPolicy |
// [0, 3]
421
policy.bits.verPolicy << 4 |
// [4, 7]
422
policy.bits.hfw << 8 |
// [8]
423
policy.bits.ctype << 9 |
// [9, 13]
424
policy.bits.wfh << 14 |
// [14]
425
policy.bits.retainSizeWhenHidden << 15 |
// [15]
426
policy.bits.verStretch << 16 |
// [16, 23]
427
policy.bits.horStretch << 24);
// [24, 31]
428
return
stream << data;
429
}
430
431
#
define
VALUE_OF_BITS
(
data
,
bitstart
,
bitcount
)
(
(
data
>>
bitstart
)
&
(
(
1
<<
bitcount
)
-
1
)
)
432
433
/*!
434
\relates QSizePolicy
435
\since 4.2
436
437
Reads the size \a policy from the data stream \a stream.
438
439
\sa{Serializing Qt Data Types}{Format of the QDataStream operators}
440
*/
441
QDataStream &operator>>(QDataStream &stream, QSizePolicy &policy)
442
{
443
quint32 data;
444
stream >> data;
445
policy.bits.horPolicy =
VALUE_OF_BITS
(data, 0, 4);
446
policy.bits.verPolicy =
VALUE_OF_BITS
(data, 4, 4);
447
policy.bits.hfw =
VALUE_OF_BITS
(data, 8, 1);
448
policy.bits.ctype =
VALUE_OF_BITS
(data, 9, 5);
449
policy.bits.wfh =
VALUE_OF_BITS
(data, 14, 1);
450
policy.bits.retainSizeWhenHidden =
VALUE_OF_BITS
(data, 15, 1);
451
policy.bits.verStretch =
VALUE_OF_BITS
(data, 16, 8);
452
policy.bits.horStretch =
VALUE_OF_BITS
(data, 24, 8);
453
return
stream;
454
}
455
#
endif
// QT_NO_DATASTREAM
456
457
#
ifndef
QT_NO_DEBUG_STREAM
458
QDebug
operator
<<(QDebug dbg,
const
QSizePolicy &p)
459
{
460
QDebugStateSaver saver(dbg);
461
dbg.nospace() <<
"QSizePolicy(horizontalPolicy = "
<< p.horizontalPolicy()
462
<<
", verticalPolicy = "
<< p.verticalPolicy() <<
')'
;
463
return
dbg;
464
}
465
#
endif
466
467
QT_END_NAMESPACE
468
469
#
include
"moc_qsizepolicy.cpp"
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
VALUE_OF_BITS
#define VALUE_OF_BITS(data, bitstart, bitcount)
Definition
qlayoutpolicy.cpp:62
operator<<
QDebug operator<<(QDebug dbg, const QSizePolicy &p)
Definition
qsizepolicy.cpp:458
qtbase
src
widgets
kernel
qsizepolicy.cpp
Generated on
for Qt by
1.18.0