diff --git a/src/corelib/global/qflags.h b/src/corelib/global/qflags.h
index feeb488acd..72b0a689ff 100644
--- a/src/corelib/global/qflags.h
+++ b/src/corelib/global/qflags.h
@@ -150,7 +150,7 @@ public:
     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);
+        return on ? (*this |= f) : (*this &= ~Int(f));
     }
 
 private:
diff --git a/tests/auto/corelib/global/qflags/tst_qflags.cpp b/tests/auto/corelib/global/qflags/tst_qflags.cpp
index 7dd1a1be01..2f1b56629a 100644
--- a/tests/auto/corelib/global/qflags/tst_qflags.cpp
+++ b/tests/auto/corelib/global/qflags/tst_qflags.cpp
@@ -290,6 +290,18 @@ void tst_QFlags::testSetFlags()
     btn.setFlag(Qt::LeftButton, false);
     QVERIFY(!btn.testFlag(Qt::LeftButton));
     QVERIFY(!btn.testFlag(Qt::MidButton));
+
+    MyStrictFlags flags;
+    flags.setFlag(MyStrictEnum::StrictOne);
+    flags.setFlag(MyStrictEnum::StrictTwo, true);
+    QVERIFY(flags.testFlag(MyStrictEnum::StrictOne));
+    QVERIFY(flags.testFlag(MyStrictEnum::StrictTwo));
+    QVERIFY(!flags.testFlag(MyStrictEnum::StrictFour));
+
+    flags.setFlag(MyStrictEnum::StrictTwo, false);
+    QVERIFY(flags.testFlag(MyStrictEnum::StrictOne));
+    QVERIFY(!flags.testFlag(MyStrictEnum::StrictTwo));
+    QVERIFY(!flags.testFlag(MyStrictEnum::StrictFour));
 }
 
 // (statically) check QTypeInfo for QFlags instantiations: