将调试中的 QUrl 可视化为 VS 中的字符串

mis*_*der 3 c++ debugging qt visual-studio

一般来说,我喜欢 QUrls,但是调试代码中有很多 QUrls 的代码真是太痛苦了,而且我在调试中看不到实际的 url 字符串,我必须在代码中对 toString 进行一些调试调用。有没有可能让它在调试手表中可见?

W.B*_*.B. 5

我知道这已经很旧了,但我自己偶然发现了这个问题并决定以某种方式解决它。至少可以说,这个解决方案很粗糙,但它确实有效。

QUrlPrivateQUrl 的问题是它的所有内部结构都使用 pimpl,并且您在调试时无权访问 的定义。一旦 QUrl 中的任何内容发生更改,此处的解决方案可能会中断,因为它基于QUrlPrivate成员的偏移量。因此,如果未来版本中出现任何问题,您可以调整偏移量,就可以了。这是从 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&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d) + 8)->d->size">{*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d) + 8)}://{*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d) + 20)}{*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d) + 24)}</DisplayString>
        <DisplayString>{*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d) + 24)}</DisplayString>
        <Expand>
            <Item Name="[scheme]">*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d) + 8)</Item>
            <Item Name="[host]">*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d) + 20)</Item>
            <Item Name="[path]">*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d) + 24)</Item>
            <Item Name="[query]">*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d) + 28)</Item>
        </Expand>
    </Type>


</AutoVisualizer>
Run Code Online (Sandbox Code Playgroud)

只需将其保存到(例如):

%USERPROFILE%\My Documents\Visual Studio 2013\Visualizers\QUrl.natvis

希望它对某人有用。

编辑:

这假设您安装了适用于其他 Qt 类型的 natvis,尤其是QString.