Fix QQmlMetaType::prettyTypeName to deal with malformed type names
We can have QML type names that are empty or end in '/'. In those cases use the QMetaObject to retrieve a more meaningful type name. Change-Id: I4dd0841de13d4e7524a104f0bbc08cb854484cfe Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
parent
d88164fdbc
commit
af9536deea
|
@ -1971,7 +1971,9 @@ QString QQmlMetaType::prettyTypeName(const QObject *object)
|
|||
const int lastSlash = typeName.lastIndexOf(QLatin1Char('/'));
|
||||
if (lastSlash != -1)
|
||||
typeName = typeName.mid(lastSlash + 1);
|
||||
} else {
|
||||
}
|
||||
|
||||
if (typeName.isEmpty()) {
|
||||
typeName = QString::fromUtf8(object->metaObject()->className());
|
||||
int marker = typeName.indexOf(QLatin1String("_QMLTYPE_"));
|
||||
if (marker != -1)
|
||||
|
@ -1982,10 +1984,12 @@ QString QQmlMetaType::prettyTypeName(const QObject *object)
|
|||
typeName = typeName.leftRef(marker) + QLatin1Char('*');
|
||||
type = QQmlMetaType::qmlType(QMetaType::type(typeName.toLatin1()));
|
||||
if (type) {
|
||||
typeName = type->qmlTypeName();
|
||||
const int lastSlash = typeName.lastIndexOf(QLatin1Char('/'));
|
||||
QString qmlTypeName = type->qmlTypeName();
|
||||
const int lastSlash = qmlTypeName.lastIndexOf(QLatin1Char('/'));
|
||||
if (lastSlash != -1)
|
||||
typeName = typeName.mid(lastSlash + 1);
|
||||
qmlTypeName = qmlTypeName.mid(lastSlash + 1);
|
||||
if (!qmlTypeName.isEmpty())
|
||||
typeName = qmlTypeName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ private slots:
|
|||
void qmlPropertyValueInterceptorCast();
|
||||
void qmlType();
|
||||
void invalidQmlTypeName();
|
||||
void prettyTypeName();
|
||||
void registrationType();
|
||||
void compositeType();
|
||||
void externalEnums();
|
||||
|
@ -72,6 +73,16 @@ public:
|
|||
};
|
||||
QML_DECLARE_TYPE(TestType);
|
||||
|
||||
class TestType2 : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
};
|
||||
|
||||
class TestType3 : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
};
|
||||
|
||||
class ExternalEnums : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
@ -214,13 +225,28 @@ void tst_qqmlmetatype::invalidQmlTypeName()
|
|||
{
|
||||
QStringList currFailures = QQmlMetaType::typeRegistrationFailures();
|
||||
QCOMPARE(qmlRegisterType<TestType>("TestNamespace", 1, 0, "Test$Type"), -1); // should fail due to invalid QML type name.
|
||||
QCOMPARE(qmlRegisterType<TestType>("Test", 1, 0, "EndingInSlash/"), -1);
|
||||
QStringList nowFailures = QQmlMetaType::typeRegistrationFailures();
|
||||
|
||||
foreach (const QString &f, currFailures)
|
||||
nowFailures.removeOne(f);
|
||||
|
||||
QCOMPARE(nowFailures.size(), 1);
|
||||
QCOMPARE(nowFailures.size(), 2);
|
||||
QCOMPARE(nowFailures.at(0), QStringLiteral("Invalid QML element name \"Test$Type\""));
|
||||
QCOMPARE(nowFailures.at(1), QStringLiteral("Invalid QML element name \"EndingInSlash/\""));
|
||||
}
|
||||
|
||||
void tst_qqmlmetatype::prettyTypeName()
|
||||
{
|
||||
TestType2 obj2;
|
||||
QCOMPARE(QQmlMetaType::prettyTypeName(&obj2), QString("TestType2"));
|
||||
QVERIFY(qmlRegisterType<TestType2>("Test", 1, 0, "") >= 0);
|
||||
QCOMPARE(QQmlMetaType::prettyTypeName(&obj2), QString("TestType2"));
|
||||
|
||||
TestType3 obj3;
|
||||
QCOMPARE(QQmlMetaType::prettyTypeName(&obj3), QString("TestType3"));
|
||||
QVERIFY(qmlRegisterType<TestType3>("Test", 1, 0, "OtherName") >= 0);
|
||||
QCOMPARE(QQmlMetaType::prettyTypeName(&obj3), QString("OtherName"));
|
||||
}
|
||||
|
||||
void tst_qqmlmetatype::isList()
|
||||
|
|
Loading…
Reference in New Issue