Make QSignalSpy copy QVariant parameters directly

Previously, a QVariant parameter would be wrapped inside a new
QVariant, and you would have to cast the QSignalSpy's QVariant to
a QVariant to get the actual value. This behavior was unintuitive
and undocumented.

Check if the parameter type is QVariant, and copy it directly if it
is. This makes the QSignalSpy's QVariant directly usable (no need to
"unwrap" the value in user code).

Existing tests that use QSignalSpy together with QVariant parameters
(such as tst_QPropertyAnimation::valueChanged()) and do cast the
QVariant parameter to a QVariant, continue to work after this change;
this is because qvariant_cast<QVariant>() returns its input value
(unchanged) when the type is not QMetaType::QVariant.

Task-number: QTBUG-21645
Change-Id: Ibfb171edd60c0d3f7ca1d5419e5c5f3d0380d5b3
Reviewed-by: Jason McDonald <jason.mcdonald@nokia.com>
bb10
Kent Hansen 2012-06-18 09:46:35 +02:00 committed by Qt by Nokia
parent 39c2fdd907
commit 9a0b7348b3
3 changed files with 18 additions and 1 deletions

5
dist/changes-5.0.0 vendored
View File

@ -499,6 +499,11 @@ QTestLib
--------
* [QTBUG-20615] Autotests can now log test output to multiple destinations
and log formats simultaneously.
* [QTBUG-21645] QSignalSpy now handles QVariant signal parameters more
intuitively; the QVariant value is copied directly, instead of being
wrapped inside a new QVariant. This means that calling
qvariant_cast<QVariant>() on the QSignalSpy item (to "unwrap" the value)
is no longer required (but still works).
QtSql
-----

View File

@ -147,7 +147,10 @@ private:
QList<QVariant> list;
for (int i = 0; i < args.count(); ++i) {
QMetaType::Type type = static_cast<QMetaType::Type>(args.at(i));
list << QVariant(type, a[i + 1]);
if (type == QMetaType::QVariant)
list << *reinterpret_cast<QVariant *>(a[i + 1]);
else
list << QVariant(type, a[i + 1]);
}
append(list);

View File

@ -157,6 +157,7 @@ signals:
void sig2(const QDateTime &dt);
void sig3(QObject *o);
void sig4(QChar c);
void sig5(const QVariant &v);
};
void tst_QSignalSpy::spyWithBasicQtClasses()
@ -168,6 +169,14 @@ void tst_QSignalSpy::spyWithBasicQtClasses()
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).count(), 1);
QCOMPARE(spy.at(0).at(0).toString(), QString("bubu"));
QSignalSpy spy2(&obj, SIGNAL(sig5(QVariant)));
QVariant val(45);
emit obj.sig5(val);
QCOMPARE(spy2.count(), 1);
QCOMPARE(spy2.at(0).count(), 1);
QCOMPARE(spy2.at(0).at(0), val);
QCOMPARE(qvariant_cast<QVariant>(spy2.at(0).at(0)), val);
}
void tst_QSignalSpy::spyWithQtClasses()