一般来说,我喜欢 QUrls,但是调试代码中有很多 QUrls 的代码非常痛苦,而且我在调试中看不到实际的 url 字符串,我不得不在代码中对 toString 进行一些调试调用。有没有可能让它在调试 watch 中可见?
最佳答案
我知道这已经过时了,但我自己偶然发现了这个问题并决定以某种方式解决它。至少可以说,该解决方案很粗糙,但它确实有效。
QUrl 的问题在于它的所有内部结构都使用了 pimpl,并且在调试时您无权访问 QUrlPrivate
的定义。一旦 QUrl 中的任何内容发生更改,此处的解决方案可能会中断,因为它基于 QUrlPrivate
成员的偏移量。因此,如果 future 版本出现任何问题,您可以调整偏移量,这样就很好了。这是从 Qt 5.3.1 开始的
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="QUrl">
<DisplayString Condition="reinterpret_cast<QString*>(reinterpret_cast<char*>(d) + 8)->d->size">{*reinterpret_cast<QString*>(reinterpret_cast<char*>(d) + 8)}://{*reinterpret_cast<QString*>(reinterpret_cast<char*>(d) + 20)}{*reinterpret_cast<QString*>(reinterpret_cast<char*>(d) + 24)}</DisplayString>
<DisplayString>{*reinterpret_cast<QString*>(reinterpret_cast<char*>(d) + 24)}</DisplayString>
<Expand>
<Item Name="[scheme]">*reinterpret_cast<QString*>(reinterpret_cast<char*>(d) + 8)</Item>
<Item Name="[host]">*reinterpret_cast<QString*>(reinterpret_cast<char*>(d) + 20)</Item>
<Item Name="[path]">*reinterpret_cast<QString*>(reinterpret_cast<char*>(d) + 24)</Item>
<Item Name="[query]">*reinterpret_cast<QString*>(reinterpret_cast<char*>(d) + 28)</Item>
</Expand>
</Type>
</AutoVisualizer>
只需将其保存到(例如):
%USERPROFILE%\My Documents\Visual Studio 2013\Visualizers\QUrl.natvis
希望对大家有用。
编辑:
这假设您安装了适用于其他 Qt 类型的 natvis,尤其是 QString
。
关于c++ - 在调试中将 QUrl 可视化为 VS 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20401195/