我的编程环境是:本地是一台win10的笔记本,使用vscode软件,远程到一台gpu服务器上,GPU服务器上有docker,通过端口,连接到了docker里面,进行代码开发,项目中是c++项目,用到了qt,发现在vscode中debug程序时,不能显示qt类的值,只显示地址值,参考了网上使用qt.natvis.xml的方法,将该文件防止在docker容器内,和防止在win机器上,均不起作用。有没有人处理过该问题,望解答。
vscode版本:1.76.1
qt版本:5.12.12
https://blog.csdn.net/qq_41539778/article/details/102926811
https://blog.csdn.net/weixin_49065061/article/details/129424255
想在使用vscode调试是,能够显示qt类的值,而不是地址
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
您可以尝试按照以下步骤来解决您的问题:
1、在您的 C++ 项目中添加一个名为 qt.natvis.xml 的文件,并将其放在项目的根目录中。
2、将以下内容添加到 qt.natvis.xml 文件中:
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="QListData">
<DisplayString>{{ size=%size%, capacity=%alloc%</DisplayString>
<Expand>
<ArrayItems>
<Size>size</Size>
<ValuePointer>array</ValuePointer>
</ArrayItems>
</Expand>
</Type>
<Type Name="QList<*>">
<DisplayString>{...}</DisplayString>
<Expand>
<Item Name="[size]">size</Item>
<Item Name="[capacity]">d->alloc</Item>
<ArrayItems>
<Size>size</Size>
<ValuePointer>d->array</ValuePointer>
</ArrayItems>
</Expand>
</Type>
<Type Name="QByteArray">
<DisplayString>{{ size=%size%, capacity=%alloc%</DisplayString>
<Expand>
<ArrayItems>
<Size>size</Size>
<ValuePointer>data</ValuePointer>
</ArrayItems>
</Expand>
</Type>
<Type Name="QString">
<DisplayString>{%s, length=%length%}</DisplayString>
<StringView>
<StringView>data->data(),static_cast<int>(length)</StringView>
<Length>length</Length>
</StringView>
</Type>
<Type Name="QVector<*>">
<DisplayString>{...}</DisplayString>
<Expand>
<Item Name="[size]">size</Item>
<Item Name="[capacity]">d->alloc</Item>
<ArrayItems>
<Size>size</Size>
<ValuePointer>d->array</ValuePointer>
</ArrayItems>
</Expand>
</Type>
</AutoVisualizer>
3、在 VS Code 中打开您的项目,然后单击左侧的调试按钮,选择“添加配置”。
4、在弹出的菜单中选择“C++”。
5、选择您的项目中的一个 C++ 文件,然后单击“创建 launch.json 文件”。
6、打开 launch.json 文件,并将以下代码添加到该文件中:
"visualizerFile": "${workspaceFolder}/qt.natvis.xml"
7、开始调试您的程序,可以看到 Qt 类的值现在已经被正确地显示了。
这个方法适用于 Visual Studio 2017 及更高版本,VS Code 也支持类似的自定义数据可视化配置,步骤大致相同。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
该回答引用ChatGPT
QMAKE_CFLAGS_DEBUG += /Z7
QMAKE_CXXFLAGS_DEBUG += /Z7
"lldb.formats": [
{
"name": "Qt",
"type": "^Q[A-Z][a-zA-Z0-9]*$",
"children": [
{
"name": "pointer",
"format": "Hex: ${var%{unsigned long long}x}"
},
{
"name": "QtCore.QRectF",
"children": [
{
"name": "x",
"format": "${var.x()}f"
},
{
"name": "y",
"format": "${var.y()}f"
},
{
"name": "width",
"format": "${var.width()}f"
},
{
"name": "height",
"format": "${var.height()}f"
}
]
}
]
}
]
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
关于在VSCode中调试Qt程序无法显示Qt对象值的问题,一般来说有两种解决方法:
方法一:使用qt5.natvis文件
这种方法需要在VSCode中配置qt5.natvis文件,该文件是Qt官方提供的,存放在Qt安装目录下的\Tools\QtCreator\share\data\qt5.natvis
。
具体操作步骤如下:
在.vscode文件夹下创建一个名为natvis的文件夹,然后将qt5.natvis文件复制进去。
在launch.json中的配置文件添加一个如下命令:
"configurationAttributes": {
"visualizerFile": "${workspaceFolder}/.vscode/natvis/qt5.natvis"已黑化的小白 }
完整的launch.json中的配置如下:
{
"version": "0.2.0",
"configurations": [
{
"type": "cppdbg",
"name": "Debug",
"request": "launch",
"program": "${workspaceFolder}/bin/Debug/myapp.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Startup",
"text": "-exec-run",
"ignoreFailures": false
}
],
"visualizerFile": "${workspaceFolder}/.vscode/natvis/qt5.natvis",
"visualizer": "Native"已黑化的小白 }
]
}
该方法需要在VSCode中安装C/C++插件。
方法二:使用qtnatvis文件
这种方法是使用qtnatvis文件对Qt对象进行可视化调试。
具体操作步骤如下:
在网上搜索“qtnatvis文件”,下载一个适用于你的Qt版本的文件,比如qtnatvisqt5.xml。
和方法一中一样,在.vscode文件夹下创建一个名为natvis的文件夹,然后将qtnatvis文件复制进去。
在launch.json中的配置文件添加一个如下命令:
"configurationAttributes": {
"visualizerFile": "${workspaceFolder}/.vscode/natvis/qtnatvisqt5.xml"已黑化的小白 }
完整的launch.json中的配置如下:
{
"version": "0.2.0",
"configurations": [
{
"type": "cppdbg",
"name": "Debug",
"request": "launch",
"program": "${workspaceFolder}/bin/Debug/myapp.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Startup",
"text": "-exec-run",
"ignoreFailures": false
}
],
"visualizerFile": "${workspaceFolder}/.vscode/natvis/qtnatvisqt5.xml",
"visualizer": "Native"已黑化的小白 }
]
}
这种方法同样需要在VSCode中安装C/C++插件。
补充:如果使用Natvis调试可以在命令行窗口中输入 -exec command执行gdb调试器命令,例如:
(gdb) -exec p myQPoint
如果我的回答解决了您的问题,请采纳!
以下内容部分参考ChatGPT模型:
首先,需要确保在Qt项目中开启了调试信息(Debug Information),可以在.pro文件中添加如下语句:
CONFIG += debug
然后,确保在.vscode/launch.json文件中配置了正确的启动参数,例如:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/build/debug/myapp",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for Qt types",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Load the Natvis file",
"text": "-natvis ${workspaceFolder}/qt.natvis"
}
]
}
]
}
其中,natvis文件需要自行创建,可以在VSCode的调试控制台中输入以下命令,将输出复制到文件中:
(gdb) info types
然后,在文件中添加Qt的类型信息,例如:
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="QString">
<DisplayString>{ d->size ? QString::fromUtf16(reinterpret_cast<const ushort*>(d->data), d->size) : QStringLiteral("") }</DisplayString>
</Type>
</AutoVisualizer>
最后,重新启动调试会话,就可以正常显示Qt类的值了。注意,如果使用的是Qt5.14及以上版本,需要使用新的natvis文件格式,具体可以参考官方文档。
如果我的建议对您有帮助、请点击采纳、祝您生活愉快