Fix QMetaType of const references

This fixes QMetaType detection of const reference arguments in signals
while connecting using the new syntax and Qt::QueuedConnection

const references should have the same QMetaType as non references.
That means we need to remove the const reference while getting the
QMetaType.

Change-Id: I9b2688da7fb9ae985aec0d8fa62a1165357ffe71
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
This commit is contained in:
Olivier Goffart 2013-02-05 14:35:10 +01:00 committed by The Qt Project
parent 8fd1330029
commit 03b2512598
3 changed files with 35 additions and 0 deletions

View File

@ -532,6 +532,9 @@ struct QMetaTypeId2
static inline Q_DECL_CONSTEXPR int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
};
template <typename T>
struct QMetaTypeId2<const T&> : QMetaTypeId2<T> {};
namespace QtPrivate {
template <typename T, bool Defined = QMetaTypeId2<T>::Defined>
struct QMetaTypeIdHelper {

View File

@ -110,6 +110,7 @@ private slots:
void saveAndLoadCustom();
void metaObject();
void constexprMetaTypeIds();
void constRefs();
};
struct Foo { int i; };
@ -1762,6 +1763,17 @@ void tst_QMetaType::constexprMetaTypeIds()
#endif
}
void tst_QMetaType::constRefs()
{
QCOMPARE(::qMetaTypeId<const int &>(), ::qMetaTypeId<int>());
QCOMPARE(::qMetaTypeId<const QString &>(), ::qMetaTypeId<QString>());
QCOMPARE(::qMetaTypeId<const CustomMovable &>(), ::qMetaTypeId<CustomMovable>());
QCOMPARE(::qMetaTypeId<const QList<CustomMovable> &>(), ::qMetaTypeId<QList<CustomMovable> >());
#if defined(Q_COMPILER_CONSTEXPR)
Q_STATIC_ASSERT(::qMetaTypeId<const int &>() == ::qMetaTypeId<int>());
#endif
}
// Compile-time test, it should be possible to register function pointer types
class Undefined;

View File

@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -1446,9 +1447,11 @@ public:
public slots:
void slot1(CustomType ct);
void slot2(const QList<CustomType> &ct);
signals:
void signal1(CustomType ct);
void signal2(const QList<CustomType> &ct);
public:
CustomType received;
@ -1457,6 +1460,8 @@ public:
void QCustomTypeChecker::slot1(CustomType ct)
{ received = ct; }
void QCustomTypeChecker::slot2(const QList< CustomType >& ct)
{ received = ct[0]; }
void tst_QObject::customTypes()
{
@ -4667,6 +4672,21 @@ void tst_QObject::customTypesPointer()
QCOMPARE(qRegisterMetaType<CustomType>("CustomType"), idx);
QCOMPARE(QMetaType::type("CustomType"), idx);
QVERIFY(QMetaType::isRegistered(idx));
// Test auto registered type (QList<CustomType>)
QList<CustomType> list;
QCOMPARE(instanceCount, 4);
list.append(t1);
QCOMPARE(instanceCount, 5);
QVERIFY(connect(&checker, &QCustomTypeChecker::signal2,
&checker, &QCustomTypeChecker::slot2, Qt::QueuedConnection));
emit checker.signal2(list);
QCOMPARE(instanceCount, 5); //because the list is implicitly shared.
list.clear();
QCOMPARE(instanceCount, 5);
QCoreApplication::processEvents();
QCOMPARE(checker.received.value(), t1.value());
QCOMPARE(instanceCount, 4);
}
QCOMPARE(instanceCount, 3);
}