Skip to content

Commit

Permalink
moc: add a test for Q_FLAG getter/setter on integers
Browse files Browse the repository at this point in the history
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 <ivan.solovev@qt.io>
(cherry picked from commit 939f7f5)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
  • Loading branch information
thiagomacieira authored and Qt Cherry-pick Bot committed Sep 24, 2024
1 parent 202acb9 commit c1b13a0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/auto/tools/moc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 23 additions & 0 deletions tests/auto/tools/moc/flags-property-integer-access.h
Original file line number Diff line number Diff line change
@@ -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 <QtCore/qobject.h>

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
22 changes: 22 additions & 0 deletions tests/auto/tools/moc/tst_moc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<ClassWithFlagsAccessAsInteger::Flags>());

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()));
Expand Down

0 comments on commit c1b13a0

Please sign in to comment.