diff options
author | Andre Somers <andre.somers@kdab.com> | 2016-02-02 10:16:24 +0100 |
---|---|---|
committer | Andre Somers <andre@familiesomers.nl> | 2016-02-08 10:25:09 +0000 |
commit | 21861e6fd1fcd1e4642f3d4c06ee7181d2c81778 (patch) | |
tree | 77ef4099210c4ba1b05dab5fd4e07aff8d7145e4 | |
parent | 74adb1900a7f6f219339bd4ed2a74b5816b63d64 (diff) |
Add setFlag method to QFlags
This makes implementing class methods that enable or disable a feature
stored in a QFlags<> member easier.
[ChangeLog][QtCore][QFlags] Added setFlag method to set or unset a flag
Task-number: QTBUG-27100
Change-Id: Ic602abbbf3100df78f21b7918573744d1bbd18c1
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
-rw-r--r-- | src/corelib/global/qflags.h | 5 | ||||
-rw-r--r-- | src/corelib/global/qglobal.cpp | 8 | ||||
-rw-r--r-- | tests/auto/corelib/global/qflags/tst_qflags.cpp | 14 |
3 files changed, 27 insertions, 0 deletions
diff --git a/src/corelib/global/qflags.h b/src/corelib/global/qflags.h index e181566fdb..b907caa9b6 100644 --- a/src/corelib/global/qflags.h +++ b/src/corelib/global/qflags.h @@ -145,6 +145,11 @@ public: Q_DECL_CONSTEXPR inline bool operator!() const Q_DECL_NOTHROW { return !i; } Q_DECL_CONSTEXPR inline bool testFlag(Enum f) const Q_DECL_NOTHROW { return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f) ); } + Q_DECL_RELAXED_CONSTEXPR inline QFlags &setFlag(Enum f, bool on = true) Q_DECL_NOTHROW + { + return on ? (*this |= f) : (*this &= ~f); + } + private: #ifdef Q_COMPILER_INITIALIZER_LISTS Q_DECL_CONSTEXPR static inline Int initializer_list_helper(typename std::initializer_list<Enum>::const_iterator it, diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 1227445f1e..a7ed29d859 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -426,6 +426,14 @@ Q_STATIC_ASSERT_X(QT_POINTER_SIZE == sizeof(void *), "QT_POINTER_SIZE defined in */ /*! + \fn QFlags QFlags::setFlag(Enum flag, bool on) const + \since 5.7 + + Sets the indicated \a flag if \a on is \c true or unsets it if + it if \a on is \c false. Returns a reference to this object. +*/ + +/*! \macro Q_DISABLE_COPY(Class) \relates QObject diff --git a/tests/auto/corelib/global/qflags/tst_qflags.cpp b/tests/auto/corelib/global/qflags/tst_qflags.cpp index 3e1b60419d..10902b6f55 100644 --- a/tests/auto/corelib/global/qflags/tst_qflags.cpp +++ b/tests/auto/corelib/global/qflags/tst_qflags.cpp @@ -38,6 +38,7 @@ private slots: void signedness(); void classEnum(); void initializerLists(); + void testSetFlags(); }; void tst_QFlags::testFlag() const @@ -278,6 +279,19 @@ void tst_QFlags::initializerLists() #endif // Q_COMPILER_INITIALIZER_LISTS } +void tst_QFlags::testSetFlags() +{ + Qt::MouseButtons btn = Qt::NoButton; + + btn.setFlag(Qt::LeftButton); + QVERIFY(btn.testFlag(Qt::LeftButton)); + QVERIFY(!btn.testFlag(Qt::MidButton)); + + btn.setFlag(Qt::LeftButton, false); + QVERIFY(!btn.testFlag(Qt::LeftButton)); + QVERIFY(!btn.testFlag(Qt::MidButton)); +} + // (statically) check QTypeInfo for QFlags instantiations: enum MyEnum { Zero, One, Two, Four=4 }; Q_DECLARE_FLAGS( MyFlags, MyEnum ) |