简体   繁体   English

在 Visual Studio Debugger 中查看 QObject 的 objectName

[英]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.不幸的是, QObject没有像人们预期的那样在调试器中可见的QString m_objectName成员。 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?有什么方法可以在运行时从 Visual Studio 调试器中查看 objectName 吗?

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.在调试 Qt 应用程序时,可能有许多特定 QObject 的实例,并且很难知道哪个实例触发了崩溃,因为该信息可能在调用堆栈中不可用。 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.然而,如果它们都被赋予了唯一的 objectNames,那么理论上可以让人们快速查明有问题的代码区域。

More Details:更多细节:

  • I'm using Qt 5.9.3 and Visual Studio 2019我正在使用 Qt 5.9.3 和 Visual Studio 2019
  • I've already installed qt5.natvis for Visual Studio (and it doesn't do this for you)我已经为 Visual Studio 安装了qt5.natvis (它不会为你做这个)
  • 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.我要求,至少,可以粘贴到任何本地QObject派生变量与 objectName 的调试器的工作 watch-expression,以显示其 objectName。
  • The ideal answer will also include the updated qt5.natvis which prominently exposes objectName for any local QObject derived variables.理想的答案还包括更新后的qt5.natvis ,它突出显示任何本地QObject派生变量的 objectName。

Debugger can't (shouldn't?) call object's member function for display purposes, as it can have side effects.调试器不能(不应该?)出于显示目的调用对象的成员 function,因为它可能有副作用。

The solution is to write or find "native visualizer" ( natvis ) for the types you are interested in.解决方案是为您感兴趣的类型编写或查找“原生可视化工具”( natvis )。

Luckily, Qt people did this: https://wiki.qt.io/IDE_Debug_Helpers .幸运的是,Qt 人这样做了: 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您可以按照此文档做更好的自定义工作: 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 ):这可能指向数据成员名称(来自qobject.cpp ):

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

Assuming Debug build ( Qt5Cored.dll ), add a variable watch as follows:假设调试构建( Qt5Cored.dll ),添加一个变量 watch 如下:

((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将“ myQtObj ”更改为您希望检查的相关QObject* (或派生的 class 扩展QObject )的局部变量名称

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.为了开发人员的方便,我将把如何将其添加到qt5.natvis作为未来的练习,或者其他回答者可能希望提供该信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM