moc: Use QMetaType::QVariant as the type for QVariant

QMetaType::QVariant has existed as a proper type for almost two
years, but the qvariant_nameToType function was written in 2006.

Using QMetaType::QVariant means QVariant can be treated just like
any other type. We can get rid of those hacky checks for LastType,
and the remaining checks become more readable.

The fact that QMetaProperty::{type,userType}() returned LastType
(0xffffffff) for QVariants was never documented (LastType itself is
internal). But there are other Qt modules that assume so. I'll fix
the ones I know about (qtdeclarative, qtscript, activeqt).

Change-Id: I799b9079bb8bbb1fe76c132525440b30415cbac5
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@nokia.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Kent Hansen 2012-01-06 13:43:40 +01:00 committed by Qt by Nokia
parent 79ebb1621e
commit 00c8984b4e
5 changed files with 20 additions and 15 deletions

View File

@ -2105,8 +2105,6 @@ QVariant::Type QMetaProperty::type() const
uint flags = mobj->d.data[handle + 2];
uint type = flags >> 24;
if (type == 0xff) // special value for QVariant
type = QVariant::LastType;
if (type)
return QVariant::Type(type);
if (isEnumType()) {
@ -2241,8 +2239,6 @@ QVariant QMetaProperty::read(const QObject *object) const
uint flags = mobj->d.data[handle + 2];
const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
t = (flags >> 24);
if (t == 0xff) // special value for QVariant
t = QVariant::LastType;
if (t == QVariant::Invalid)
t = QMetaType::type(typeName);
if (t == QVariant::Invalid)
@ -2262,7 +2258,7 @@ QVariant QMetaProperty::read(const QObject *object) const
int status = -1;
QVariant value;
void *argv[] = { 0, &value, &status };
if (t == QVariant::LastType) {
if (t == QMetaType::QVariant) {
argv[0] = &value;
} else {
value = QVariant(t, (void*)0);
@ -2273,7 +2269,7 @@ QVariant QMetaProperty::read(const QObject *object) const
if (status != -1)
return value;
if (t != QVariant::LastType && argv[0] != value.data())
if (t != QMetaType::QVariant && argv[0] != value.data())
// pointer or reference
return QVariant((QVariant::Type)t, argv[0]);
return value;
@ -2312,8 +2308,6 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
int handle = priv(mobj->d.data)->propertyData + 3*idx;
uint flags = mobj->d.data[handle + 2];
t = flags >> 24;
if (t == 0xff) // special value for QVariant
t = QVariant::LastType;
if (t == QVariant::Invalid) {
const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
const char *vtypeName = value.typeName();
@ -2324,7 +2318,7 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
}
if (t == QVariant::Invalid)
return false;
if (t != QVariant::LastType && t != (uint)value.userType() && (t < QMetaType::User && !v.convert((QVariant::Type)t)))
if (t != QMetaType::QVariant && t != (uint)value.userType() && (t < QMetaType::User && !v.convert((QVariant::Type)t)))
return false;
}
@ -2338,7 +2332,7 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
// interception of property writes.
int flags = 0;
void *argv[] = { 0, &v, &status, &flags };
if (t == QVariant::LastType)
if (t == QMetaType::QVariant)
argv[0] = &v;
else
argv[0] = v.data();

View File

@ -80,8 +80,6 @@ uint qvariant_nameToType(const char* name)
if (!name)
return 0;
if (strcmp(name, "QVariant") == 0)
return 0xffffffff;
if (strcmp(name, "QCString") == 0)
return QMetaType::QByteArray;
if (strcmp(name, "Q_LLONG") == 0)

View File

@ -54,8 +54,6 @@ uint qvariant_nameToType(const char* name)
if (!name)
return 0;
if (strcmp(name, "QVariant") == 0)
return 0xffffffff;
if (strcmp(name, "QCString") == 0)
return QMetaType::QByteArray;
if (strcmp(name, "Q_LLONG") == 0)

View File

@ -57,6 +57,7 @@ private slots:
void signal();
void constructor();
void property();
void variantProperty();
void notifySignal();
void enumerator();
void classInfo();
@ -727,6 +728,20 @@ void tst_QMetaObjectBuilder::property()
QCOMPARE(builder.method(0).signature(), QByteArray("propChanged(QString)"));
}
void tst_QMetaObjectBuilder::variantProperty()
{
QMetaObjectBuilder builder;
builder.addProperty("variant", "const QVariant &");
QMetaObject *meta = builder.toMetaObject();
QMetaProperty prop = meta->property(meta->propertyOffset());
QCOMPARE(QMetaType::Type(prop.type()), QMetaType::QVariant);
QCOMPARE(QMetaType::Type(prop.userType()), QMetaType::QVariant);
QCOMPARE(QByteArray(prop.typeName()), QByteArray("QVariant"));
qFree(meta);
}
void tst_QMetaObjectBuilder::notifySignal()
{
QMetaObjectBuilder builder;

View File

@ -1671,7 +1671,7 @@ void tst_QObject::property()
const int idx = mo->indexOfProperty("variant");
QVERIFY(idx != -1);
QVERIFY(mo->property(idx).type() == QVariant::LastType);
QCOMPARE(QMetaType::Type(mo->property(idx).type()), QMetaType::QVariant);
QCOMPARE(object.property("variant"), QVariant());
QVariant variant1(42);
QVariant variant2("string");