Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • I'm using Qt 5.9.3 and Visual Studio 2019
  • I've already installed qt5.natvis for Visual Studio (and it doesn't do this for you)
  • I'm asking for, at minimum, a working watch-expression which can be pasted into the debugger for any local QObject derived variable with an objectName, in order to display its objectName.
  • The ideal answer will also include the updated qt5.natvis which prominently exposes objectName for any local QObject derived variables.
like image 890
Patrick Parker Avatar asked Jul 20 '26 00:07

Patrick Parker


1 Answers

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.

like image 136
Patrick Parker Avatar answered Jul 21 '26 14:07

Patrick Parker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!