Patrick Parker
Patrick Parker

Reputation: 4959

View QObject's objectName in Visual Studio Debugger

Unfortunately QObject has nothing like a QString m_objectName member visible in the debugger as one might expect. Instead, all of the implementation data is hidden behind opaque pointers. Is there any way to view the objectName at runtime from within the Visual Studio debugger?

Background:

When debugging a Qt Application, there may be many instances of a particular QObject and it can be difficult to know which one triggered a crash, since that information may not available in the call stack. However in the case where they have all been given unique objectNames, that could in theory allow one to quickly pinpoint the problematic code area.

More Details:

Upvotes: 3

Views: 1017

Answers (2)

Patrick Parker
Patrick Parker

Reputation: 4959

Assuming Debug build (Qt5Cored.dll), add a variable watch as follows:

((Qt5Cored.dll!QObjectPrivate*)myQtObj->d_ptr.d)->extraData->objectName
  • change "myQtObj" to the local variable name of the relevant QObject* (or derived class extending QObject) that you wish to inspect

I will leave how to add this to qt5.natvis for developer convenience as a future exercise, or another answerer may wish to contribute that information.

Upvotes: 1

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

Debugger can't (shouldn't?) call object's member function for display purposes, as it can have side effects.

The solution is to write or find "native visualizer" (natvis) for the types you are interested in.

Luckily, Qt people did this: https://wiki.qt.io/IDE_Debug_Helpers.

You may be able to do a better custom job following this doc: https://learn.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects?view=vs-2019

This may point to the data member name (from qobject.cpp) :

QString QObject::objectName() const
{
    Q_D(const QObject);
    return d->extraData ? d->extraData->objectName : QString();
}

Upvotes: 0

Related Questions