Skip to content

Commit

Permalink
Show classnames next to objectNames, when different
Browse files Browse the repository at this point in the history
  Property     | Value                        | Type     | Class
Before:
  parent       | m_loadingWidget              | QObject* | QObject
After:
  parent       | m_loadingWidget (QSvgWidget) | QObject* | QObject

For consistency, change the case without object name from
"QFrame[this=0xdcd22e0]" to "0xdcd22e0 (QFrame)", as suggested
by ferdnyc.

The change propagated to other classes to fix const [in]correctness
(the const_cast is only because of QHash API, it shouldn't be necessary
for the caller of those methods).
  • Loading branch information
dfaure authored and dfaure-kdab committed Nov 29, 2023
1 parent a946b5d commit b40e554
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
8 changes: 4 additions & 4 deletions core/objectdataprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ QString ObjectDataProvider::name(const QObject *obj)
return name;
}

QString ObjectDataProvider::typeName(QObject *obj)
QString ObjectDataProvider::typeName(const QObject *obj)
{
if (!obj)
return QString();
Expand All @@ -61,7 +61,7 @@ QString ObjectDataProvider::typeName(QObject *obj)
return obj->metaObject()->className();
}

QString ObjectDataProvider::shortTypeName(QObject *obj)
QString ObjectDataProvider::shortTypeName(const QObject *obj)
{
if (!obj)
return QString();
Expand All @@ -74,7 +74,7 @@ QString ObjectDataProvider::shortTypeName(QObject *obj)
return obj->metaObject()->className();
}

SourceLocation ObjectDataProvider::creationLocation(QObject *obj)
SourceLocation ObjectDataProvider::creationLocation(const QObject *obj)
{
SourceLocation loc;
if (!obj)
Expand All @@ -90,7 +90,7 @@ SourceLocation ObjectDataProvider::creationLocation(QObject *obj)
return loc;
}

SourceLocation ObjectDataProvider::declarationLocation(QObject *obj)
SourceLocation ObjectDataProvider::declarationLocation(const QObject *obj)
{
SourceLocation loc;
if (!obj)
Expand Down
16 changes: 8 additions & 8 deletions core/objectdataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class GAMMARAY_CORE_EXPORT AbstractObjectDataProvider
/*! Returns a name or identifier for @p obj. */
virtual QString name(const QObject *obj) const = 0;
/*! Returns the full name of the type of @p obj. */
virtual QString typeName(QObject *obj) const = 0;
virtual QString typeName(const QObject *obj) const = 0;
/*! Returns a shortened type name (e.g. excluding namespaces) for @p obj. */
virtual QString shortTypeName(QObject *obj) const = 0;
virtual QString shortTypeName(const QObject *obj) const = 0;
/*! Returns the source location where @p obj has been created. */
virtual SourceLocation creationLocation(QObject *obj) const = 0;
virtual SourceLocation creationLocation(const QObject *obj) const = 0;
/*! Returns the source location where the type of @p obj has been declared. */
virtual SourceLocation declarationLocation(QObject *obj) const = 0;
virtual SourceLocation declarationLocation(const QObject *obj) const = 0;

private:
Q_DISABLE_COPY(AbstractObjectDataProvider)
Expand All @@ -61,16 +61,16 @@ GAMMARAY_CORE_EXPORT void registerProvider(AbstractObjectDataProvider *provider)
GAMMARAY_CORE_EXPORT QString name(const QObject *obj);

/*! Returns the type name of @p obj. */
GAMMARAY_CORE_EXPORT QString typeName(QObject *obj);
GAMMARAY_CORE_EXPORT QString typeName(const QObject *obj);

/*! Returns the short type name of @p obj. */
GAMMARAY_CORE_EXPORT QString shortTypeName(QObject *obj);
GAMMARAY_CORE_EXPORT QString shortTypeName(const QObject *obj);

/*! Returns the source location where this object was created, if known. */
GAMMARAY_CORE_EXPORT SourceLocation creationLocation(QObject *obj);
GAMMARAY_CORE_EXPORT SourceLocation creationLocation(const QObject *obj);

/*! Returns the source location where the type of this object was declared, if known. */
GAMMARAY_CORE_EXPORT SourceLocation declarationLocation(QObject *obj);
GAMMARAY_CORE_EXPORT SourceLocation declarationLocation(const QObject *obj);
}
}

Expand Down
7 changes: 4 additions & 3 deletions core/probe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1009,14 +1009,15 @@ void Probe::executeSignalCallback(const Func &func)
func);
}

SourceLocation Probe::objectCreationSourceLocation(QObject *object)
SourceLocation Probe::objectCreationSourceLocation(const QObject *object)
{
if (!s_listener()->constructionBacktracesForObjects.contains(object)) {
QObject *key = const_cast<QObject *>(object);
if (!s_listener()->constructionBacktracesForObjects.contains(key)) {
IF_DEBUG(std::cout << "No backtrace for object available" << object << "." << std::endl;)
return SourceLocation();
}

const auto &st = s_listener()->constructionBacktracesForObjects.value(object);
const auto &st = s_listener()->constructionBacktracesForObjects.value(key);
int distanceToQObject = 0;

const QMetaObject *metaObject = object->metaObject();
Expand Down
2 changes: 1 addition & 1 deletion core/probe.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class GAMMARAY_CORE_EXPORT Probe : public QObject
void registerSignalSpyCallbackSet(const SignalSpyCallbackSet &callbacks);

/*! Returns the source code location @p object was created at. */
static SourceLocation objectCreationSourceLocation(QObject *object);
static SourceLocation objectCreationSourceLocation(const QObject *object);
/*! Returns the entire stack trace for the creation of @p object. */
static Execution::Trace objectCreationStackTrace(QObject *object);

Expand Down
9 changes: 6 additions & 3 deletions core/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ using namespace std;
QString Util::displayString(const QObject *object)
{
if (!object)
return QStringLiteral("QObject(0x0)");
return QStringLiteral("0x0 (QObject)");
const auto name = ObjectDataProvider::name(object);
if (name.isEmpty())
return QStringLiteral("%1[this=%2]").arg(object->metaObject()->className(), addressToString(object));
return name;
return QStringLiteral("%1 (%2)").arg(addressToString(object), object->metaObject()->className());
const auto typeName = ObjectDataProvider::typeName(object);
if (name == typeName)
return name;
return QStringLiteral("%1 (%2)").arg(ObjectDataProvider::typeName(object), name);
}

QString Util::shortDisplayString(const QObject *object)
Expand Down
18 changes: 9 additions & 9 deletions plugins/qmlsupport/qmlsupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ class QmlObjectDataProvider : public AbstractObjectDataProvider
{
public:
QString name(const QObject *obj) const override;
QString typeName(QObject *obj) const override;
QString shortTypeName(QObject *obj) const override;
SourceLocation creationLocation(QObject *obj) const override;
SourceLocation declarationLocation(QObject *obj) const override;
QString typeName(const QObject *obj) const override;
QString shortTypeName(const QObject *obj) const override;
SourceLocation creationLocation(const QObject *obj) const override;
SourceLocation declarationLocation(const QObject *obj) const override;
};
}

Expand All @@ -202,7 +202,7 @@ QString QmlObjectDataProvider::name(const QObject *obj) const
return ctx->nameForObject(const_cast<QObject *>(obj));
}

QString QmlObjectDataProvider::typeName(QObject *obj) const
QString QmlObjectDataProvider::typeName(const QObject *obj) const
{
Q_ASSERT(obj);

Expand Down Expand Up @@ -234,7 +234,7 @@ QString QmlObjectDataProvider::typeName(QObject *obj) const
return QString();
}

QString QmlObjectDataProvider::shortTypeName(QObject *obj) const
QString QmlObjectDataProvider::shortTypeName(const QObject *obj) const
{
auto n = typeName(obj);
const auto isQmlType = !n.isEmpty();
Expand All @@ -255,13 +255,13 @@ QString QmlObjectDataProvider::shortTypeName(QObject *obj) const
return isQmlType ? n : QString(); // let somebody else handle shortening of non-QML names
}

SourceLocation QmlObjectDataProvider::creationLocation(QObject *obj) const
SourceLocation QmlObjectDataProvider::creationLocation(const QObject *obj) const
{
SourceLocation loc;

auto objectData = QQmlData::get(obj);
if (!objectData) {
if (auto context = qobject_cast<QQmlContext *>(obj)) {
if (auto context = qobject_cast<const QQmlContext *>(obj)) {
loc.setUrl(context->baseUrl());
}
return loc;
Expand All @@ -277,7 +277,7 @@ SourceLocation QmlObjectDataProvider::creationLocation(QObject *obj) const
return loc;
}

SourceLocation QmlObjectDataProvider::declarationLocation(QObject *obj) const
SourceLocation QmlObjectDataProvider::declarationLocation(const QObject *obj) const
{
Q_ASSERT(obj);

Expand Down

0 comments on commit b40e554

Please sign in to comment.