diff options
author | Tobias Koenig <tobias.koenig@kdab.com> | 2015-09-24 12:45:22 +0200 |
---|---|---|
committer | Tobias Koenig <tobias.koenig@kdab.com> | 2015-09-24 11:11:32 +0000 |
commit | fe7d35e3cbc7b6db42fbc66e26a63d321260cea0 (patch) | |
tree | b055a92ff980fa9f57bd5f6b12355aa1045f93a1 /src/qml/qml/qqmlmetatype.cpp | |
parent | 0e90fac4afeda889c38a8c1998fdba92e9138559 (diff) |
Factor out method to get pretty QML type name
Factor out the code to get the pretty QML type name
for an QObject from ~QQmlInfo and put it into
QQmlMetaType::prettyTypeName() method.
Change-Id: I54ab0c49ba4a52074877447ef67708104f954f2d
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Diffstat (limited to 'src/qml/qml/qqmlmetatype.cpp')
-rw-r--r-- | src/qml/qml/qqmlmetatype.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/qml/qml/qqmlmetatype.cpp b/src/qml/qml/qqmlmetatype.cpp index b173bc8bec..04c001d305 100644 --- a/src/qml/qml/qqmlmetatype.cpp +++ b/src/qml/qml/qqmlmetatype.cpp @@ -1908,4 +1908,43 @@ const QQmlPrivate::CachedQmlUnit *QQmlMetaType::findCachedCompilationUnit(const return 0; } +/*! + Returns the pretty QML type name (e.g. 'Item' instead of 'QtQuickItem') for the given object. + */ +QString QQmlMetaType::prettyTypeName(const QObject *object) +{ + QString typeName; + + if (!object) + return typeName; + + const QQmlType *type = QQmlMetaType::qmlType(object->metaObject()); + if (type) { + typeName = type->qmlTypeName(); + const int lastSlash = typeName.lastIndexOf(QLatin1Char('/')); + if (lastSlash != -1) + typeName = typeName.mid(lastSlash + 1); + } else { + typeName = QString::fromUtf8(object->metaObject()->className()); + int marker = typeName.indexOf(QLatin1String("_QMLTYPE_")); + if (marker != -1) + typeName = typeName.left(marker); + + marker = typeName.indexOf(QLatin1String("_QML_")); + if (marker != -1) { + typeName = typeName.left(marker); + typeName += QLatin1Char('*'); + type = QQmlMetaType::qmlType(QMetaType::type(typeName.toLatin1())); + if (type) { + typeName = type->qmlTypeName(); + const int lastSlash = typeName.lastIndexOf(QLatin1Char('/')); + if (lastSlash != -1) + typeName = typeName.mid(lastSlash + 1); + } + } + } + + return typeName; +} + QT_END_NAMESPACE |