diff --git a/src/widgets/kernel/qsizepolicy.cpp b/src/widgets/kernel/qsizepolicy.cpp index a03523d2af..b08a9abb1e 100644 --- a/src/widgets/kernel/qsizepolicy.cpp +++ b/src/widgets/kernel/qsizepolicy.cpp @@ -254,6 +254,11 @@ QSizePolicy::ControlType QSizePolicy::controlType() const \sa QStyle::layoutSpacing() */ void QSizePolicy::setControlType(ControlType type) +{ + bits.ctype = toControlTypeFieldValue(type); +} + +quint32 QSizePolicy::toControlTypeFieldValue(ControlType type) Q_DECL_NOTHROW { /* The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10, @@ -271,10 +276,8 @@ void QSizePolicy::setControlType(ControlType type) int i = 0; while (true) { - if (type & (0x1 << i)) { - bits.ctype = i; - return; - } + if (type & (0x1 << i)) + return i; ++i; } } diff --git a/src/widgets/kernel/qsizepolicy.h b/src/widgets/kernel/qsizepolicy.h index eceb08259e..83ce5853ab 100644 --- a/src/widgets/kernel/qsizepolicy.h +++ b/src/widgets/kernel/qsizepolicy.h @@ -119,12 +119,19 @@ public: QT_SIZEPOLICY_CONSTEXPR QSizePolicy() : data(0) { } +#ifdef Q_COMPILER_UNIFORM_INIT + QT_SIZEPOLICY_CONSTEXPR QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType) + : bits{0, 0, quint32(horizontal), quint32(vertical), + type == DefaultType ? 0 : toControlTypeFieldValue(type), 0, 0, 0} + {} +#else QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType) : data(0) { bits.horPolicy = horizontal; bits.verPolicy = vertical; setControlType(type); } +#endif // uniform-init QT_SIZEPOLICY_CONSTEXPR Policy horizontalPolicy() const { return static_cast(bits.horPolicy); } QT_SIZEPOLICY_CONSTEXPR Policy verticalPolicy() const { return static_cast(bits.verPolicy); } ControlType controlType() const; @@ -176,6 +183,8 @@ private: struct Bits; QT_SIZEPOLICY_CONSTEXPR explicit QSizePolicy(Bits b) Q_DECL_NOTHROW : bits(b) { } + static quint32 toControlTypeFieldValue(ControlType type) Q_DECL_NOTHROW; + struct Bits { quint32 horStretch : 8; quint32 verStretch : 8; diff --git a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp index 98b765a6c6..4ae1f02ce2 100644 --- a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp +++ b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp @@ -113,6 +113,8 @@ void tst_QSizePolicy::constExpr() // check that certain ctors are constexpr (compile-only): { Q_CONSTEXPR QSizePolicy sp; Q_UNUSED(sp); } { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(); Q_UNUSED(sp); } + { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); Q_UNUSED(sp); } + { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding, QSizePolicy::DefaultType); Q_UNUSED(sp); } #else QSKIP("QSizePolicy cannot be constexpr with this version of the compiler."); #endif