From c1b13a0c5ffa2589d9fa6e11d4a215fa38e4b02a Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 10 Sep 2024 10:59:48 -0700 Subject: [PATCH] moc: add a test for Q_FLAG getter/setter on integers This is legacy behavior from Qt 3, before we had QFlags and before QVariant could support user types. I cannot find any instance of a getter returning an integer in current Qt or Qt Creator code. And note this only compiles if the flags type with Q_FLAG - not Q_ENUM. The content is wrapped as Qt 6.x only so it can be removed in Qt 7.0. The deprecation warning will come in a later commit, for 6.9. Change-Id: Ie3ddd8025e3b4387866efffd8e8d46c3daa0dff2 Reviewed-by: Ivan Solovev (cherry picked from commit 939f7f56227e65c9797d17640a7b9c29166efc44) Reviewed-by: Qt Cherry-pick Bot --- tests/auto/tools/moc/CMakeLists.txt | 4 ++++ .../tools/moc/flags-property-integer-access.h | 23 +++++++++++++++++++ tests/auto/tools/moc/tst_moc.cpp | 22 ++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 tests/auto/tools/moc/flags-property-integer-access.h diff --git a/tests/auto/tools/moc/CMakeLists.txt b/tests/auto/tools/moc/CMakeLists.txt index a8df73bdf4c..92d022c5559 100644 --- a/tests/auto/tools/moc/CMakeLists.txt +++ b/tests/auto/tools/moc/CMakeLists.txt @@ -92,6 +92,10 @@ qt_internal_extend_target(tst_moc CONDITION CMAKE_CROSSCOMPILING MOC_CROSS_COMPILED ) +if (${PROJECT_VERSION_MAJOR} LESS 7) + qt_internal_extend_target(tst_moc SOURCES flags-property-integer-access.h) +endif() + if (UNIX AND (CLANG OR GCC OR QCC)) qt_wrap_cpp(os9_moc os9-newlines.h) endif() diff --git a/tests/auto/tools/moc/flags-property-integer-access.h b/tests/auto/tools/moc/flags-property-integer-access.h new file mode 100644 index 00000000000..4d972e13db8 --- /dev/null +++ b/tests/auto/tools/moc/flags-property-integer-access.h @@ -0,0 +1,23 @@ +// Copyright (C) 2024 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#ifndef FLAGS_PROPERTY_INTEGER_ACCESS +#define FLAGS_PROPERTY_INTEGER_ACCESS +#include + +class ClassWithFlagsAccessAsInteger : public QObject +{ + Q_OBJECT +public: + enum Flag { F1 = 1, F2 = 2 }; + Q_DECLARE_FLAGS(Flags, Flag) + Q_FLAG(Flags) + Q_PROPERTY(Flags flagsValue READ flagsValue WRITE setFlagsValue) + uint flagsValue() const { return f; } + void setFlagsValue(uint v) { f = v; } + +private: + uint f = 0; +}; + +#endif // FLAGS_PROPERTY_INTEGER_ACCESS diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index 922dabbaebb..eefcd28e589 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -777,6 +777,9 @@ private slots: void uLongLong(); void inputFileNameWithDotsButNoExtension(); void userProperties(); +#if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0) + void integerAccessFlagsProperties(); +#endif void supportConstSignals(); void task87883(); void multilineComments(); @@ -1077,6 +1080,25 @@ void tst_Moc::userProperties() QVERIFY(!property.isUser()); } +#if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0) +#include "flags-property-integer-access.h" + +void tst_Moc::integerAccessFlagsProperties() +{ + ClassWithFlagsAccessAsInteger o; + + const QMetaObject *mobj = &ClassWithFlagsAccessAsInteger::staticMetaObject; + QMetaProperty property = mobj->property(mobj->indexOfProperty("flagsValue")); + QVERIFY(property.isValid()); + QCOMPARE(property.metaType(), QMetaType::fromType()); + + QVariant v = property.read(&o); + QCOMPARE(v, 0); + property.write(&o, QVariant::fromValue(ClassWithFlagsAccessAsInteger::F2)); + QCOMPARE(o.flagsValue(), ClassWithFlagsAccessAsInteger::F2); +} +#endif + void tst_Moc::supportConstSignals() { QSignalSpy spy1(this, SIGNAL(constSignal1()));