Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsizepolicy.h
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
4#ifndef QSIZEPOLICY_H
5#define QSIZEPOLICY_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtCore/qobject.h>
9#include <QtCore/qalgorithms.h>
10#include <QtCore/qhashfunctions.h>
11
13
14class QVariant;
15class QSizePolicy;
16
17class Q_WIDGETS_EXPORT QSizePolicy
18{
20
21public:
23 GrowFlag = 1,
24 ExpandFlag = 2,
25 ShrinkFlag = 4,
26 IgnoreFlag = 8
27 };
28
29 enum Policy {
30 Fixed = 0,
31 Minimum = GrowFlag,
32 Maximum = ShrinkFlag,
33 Preferred = GrowFlag | ShrinkFlag,
34 MinimumExpanding = GrowFlag | ExpandFlag,
35 Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
36 Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
37 };
38 Q_ENUM(Policy)
39
41 DefaultType = 0x00000001,
42 ButtonBox = 0x00000002,
43 CheckBox = 0x00000004,
44 ComboBox = 0x00000008,
45 Frame = 0x00000010,
46 GroupBox = 0x00000020,
47 Label = 0x00000040,
48 Line = 0x00000080,
49 LineEdit = 0x00000100,
50 PushButton = 0x00000200,
51 RadioButton = 0x00000400,
52 Slider = 0x00000800,
53 SpinBox = 0x00001000,
54 TabWidget = 0x00002000,
55 ToolButton = 0x00004000
56 };
57 Q_DECLARE_FLAGS(ControlTypes, ControlType)
58 Q_FLAG(ControlTypes)
59
60 constexpr QSizePolicy() noexcept : data(0) { }
61
62 constexpr QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType) noexcept
63 : bits{0, 0, quint32(horizontal), quint32(vertical),
64 type == DefaultType ? 0 : toControlTypeFieldValue(type), 0, 0, 0}
65 {}
66 constexpr Policy horizontalPolicy() const noexcept { return static_cast<Policy>(bits.horPolicy); }
67 constexpr Policy verticalPolicy() const noexcept { return static_cast<Policy>(bits.verPolicy); }
68 ControlType controlType() const noexcept;
69
70 constexpr void setHorizontalPolicy(Policy d) noexcept { bits.horPolicy = d; }
71 constexpr void setVerticalPolicy(Policy d) noexcept { bits.verPolicy = d; }
72 void setControlType(ControlType type) noexcept;
73
74 // ### Qt 7: consider making Policy a QFlags and removing these casts
75 constexpr Qt::Orientations expandingDirections() const noexcept {
76 return ( (verticalPolicy() & static_cast<Policy>(ExpandFlag)) ? Qt::Vertical : Qt::Orientations() )
77 | ( (horizontalPolicy() & static_cast<Policy>(ExpandFlag)) ? Qt::Horizontal : Qt::Orientations() ) ;
78 }
79
80 constexpr void setHeightForWidth(bool b) noexcept { bits.hfw = b; }
81 constexpr bool hasHeightForWidth() const noexcept { return bits.hfw; }
82 constexpr void setWidthForHeight(bool b) noexcept { bits.wfh = b; }
83 constexpr bool hasWidthForHeight() const noexcept { return bits.wfh; }
84
85 constexpr bool operator==(const QSizePolicy& s) const noexcept { return data == s.data; }
86 constexpr bool operator!=(const QSizePolicy& s) const noexcept { return data != s.data; }
87
88 friend Q_DECL_CONST_FUNCTION size_t qHash(QSizePolicy key, size_t seed = 0) noexcept { return qHash(key.data, seed); }
89
90 operator QVariant() const;
91
92 constexpr int horizontalStretch() const noexcept { return static_cast<int>(bits.horStretch); }
93 constexpr int verticalStretch() const noexcept { return static_cast<int>(bits.verStretch); }
94 constexpr void setHorizontalStretch(int stretchFactor) { bits.horStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
95 constexpr void setVerticalStretch(int stretchFactor) { bits.verStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
96
97 constexpr bool retainSizeWhenHidden() const noexcept { return bits.retainSizeWhenHidden; }
98 constexpr void setRetainSizeWhenHidden(bool retainSize) noexcept { bits.retainSizeWhenHidden = retainSize; }
99
100 constexpr void transpose() noexcept { *this = transposed(); }
101 [[nodiscard]] constexpr QSizePolicy transposed() const noexcept
102 {
103 return QSizePolicy(bits.transposed());
104 }
105
106private:
107#ifndef QT_NO_DATASTREAM
108 friend Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
109 friend Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
110#endif
111 constexpr QSizePolicy(int i) noexcept : data(i) { }
112 struct Bits;
113 constexpr explicit QSizePolicy(Bits b) noexcept : bits(b) { }
114
115 static constexpr quint32 toControlTypeFieldValue(ControlType type) noexcept
116 {
117 /*
118 The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10,
119 etc. In memory, we pack it onto the available bits (CTSize) in
120 setControlType(), and unpack it here.
121
122 Example:
123
124 0x00000001 maps to 0
125 0x00000002 maps to 1
126 0x00000004 maps to 2
127 0x00000008 maps to 3
128 etc.
129 */
130
131 return qCountTrailingZeroBits(static_cast<quint32>(type));
132 }
133
134 struct Bits {
135 quint32 horStretch : 8;
136 quint32 verStretch : 8;
137 quint32 horPolicy : 4;
138 quint32 verPolicy : 4;
139 quint32 ctype : 5;
140 quint32 hfw : 1;
141 quint32 wfh : 1;
142 quint32 retainSizeWhenHidden : 1;
143
144 constexpr Bits transposed() const noexcept
145 {
146 return {verStretch, // \ swap
147 horStretch, // /
148 verPolicy, // \ swap
149 horPolicy, // /
150 ctype,
151 hfw, // \ don't swap (historic behavior)
152 wfh, // /
153 retainSizeWhenHidden};
154 }
155 };
156 union {
157 Bits bits;
159 };
160};
161
163
164Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
166
167#ifndef QT_NO_DATASTREAM
168Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
170#endif
171
172#ifndef QT_NO_DEBUG_STREAM
173Q_WIDGETS_EXPORT QDebug operator<<(QDebug dbg, const QSizePolicy &);
174#endif
175
177
178#endif // QSIZEPOLICY_H
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
The QSizePolicy class is a layout attribute describing horizontal and vertical resizing policy.
Definition qsizepolicy.h:18
constexpr Policy verticalPolicy() const noexcept
Returns the vertical component of the size policy.
Definition qsizepolicy.h:67
constexpr QSizePolicy transposed() const noexcept
quint32 data
constexpr QSizePolicy(Policy horizontal, Policy vertical, ControlType type=DefaultType) noexcept
Definition qsizepolicy.h:62
constexpr bool hasWidthForHeight() const noexcept
Returns true if the widget's width depends on its height; otherwise returns false.
Definition qsizepolicy.h:83
constexpr void setWidthForHeight(bool b) noexcept
Sets the flag determining whether the widget's width depends on its height, to dependent.
Definition qsizepolicy.h:82
constexpr bool retainSizeWhenHidden() const noexcept
Definition qsizepolicy.h:97
constexpr void transpose() noexcept
Swaps the horizontal and vertical policies and stretches.
PolicyFlag
These flags are combined together to form the various \l{Policy} values:
Definition qsizepolicy.h:22
constexpr Qt::Orientations expandingDirections() const noexcept
Returns whether a widget can make use of more space than the QWidget::sizeHint() function indicates.
Definition qsizepolicy.h:75
constexpr Policy horizontalPolicy() const noexcept
Returns the horizontal component of the size policy.
Definition qsizepolicy.h:66
friend Q_DECL_CONST_FUNCTION size_t qHash(QSizePolicy key, size_t seed=0) noexcept
Definition qsizepolicy.h:88
constexpr void setVerticalStretch(int stretchFactor)
Sets the vertical stretch factor of the size policy to the given stretchFactor.
Definition qsizepolicy.h:95
constexpr bool hasHeightForWidth() const noexcept
Returns true if the widget's preferred height depends on its width; otherwise returns false.
Definition qsizepolicy.h:81
constexpr void setVerticalPolicy(Policy d) noexcept
Sets the vertical component to the given policy.
Definition qsizepolicy.h:71
constexpr bool operator==(const QSizePolicy &s) const noexcept
Returns true if this policy is equal to other; otherwise returns false.
Definition qsizepolicy.h:85
constexpr bool operator!=(const QSizePolicy &s) const noexcept
Returns true if this policy is different from other; otherwise returns false.
Definition qsizepolicy.h:86
constexpr void setHorizontalStretch(int stretchFactor)
Sets the horizontal stretch factor of the size policy to the given stretchFactor.
Definition qsizepolicy.h:94
constexpr int horizontalStretch() const noexcept
Returns the horizontal stretch factor of the size policy.
Definition qsizepolicy.h:92
constexpr void setRetainSizeWhenHidden(bool retainSize) noexcept
Definition qsizepolicy.h:98
Policy
This enum describes the various per-dimension sizing types used when constructing a QSizePolicy.
Definition qsizepolicy.h:29
constexpr int verticalStretch() const noexcept
Returns the vertical stretch factor of the size policy.
Definition qsizepolicy.h:93
constexpr void setHeightForWidth(bool b) noexcept
Sets the flag determining whether the widget's preferred height depends on its width,...
Definition qsizepolicy.h:80
\inmodule QtCore
Definition qvariant.h:65
Combined button and popup list for selecting options.
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
constexpr uint qCountTrailingZeroBits(quint32 v) noexcept
#define Q_DECL_CONST_FUNCTION
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Flags, Enum)
Definition qflags.h:241
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
GLboolean GLboolean GLboolean b
GLuint64 key
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLdouble s
[6]
Definition qopenglext.h:235
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
Q_WIDGETS_EXPORT QDataStream & operator>>(QDataStream &, QSizePolicy &)
Q_WIDGETS_EXPORT QDataStream & operator<<(QDataStream &, const QSizePolicy &)
#define Q_ENUM(x)
#define Q_FLAG(x)
#define Q_GADGET
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
unsigned int quint32
Definition qtypes.h:50
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QDataStream & operator>>(QDataStream &in, MyClass &myObj)