c++ - 在调试中将 QUrl 可视化为 VS 中的字符串

标签 c++ visual-studio qt debugging

一般来说,我喜欢 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&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>

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

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

希望对大家有用。

编辑:

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

关于c++ - 在调试中将 QUrl 可视化为 VS 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20401195/

相关文章:

c++ - 当依赖库之一无法编译时,如何防止 Visual Studio 构建我的整个解决方案?

visual-studio - 使用制表符作为空格时删除制表符

c++ - 如何使用 toInt 从 Qt 中的 QByteArray 中提取整数?

c++ - 这个例子中的qGetPtrHelper有什么用?

c++ - 将 fstream ">>"作为函数参数传递

c++ - 修改 std::set 的元素 - 定义的行为?

c++ std::copy 类型转换为派生类可能吗?

c# - Xamarin Forms 按钮单击事件在运行时崩溃

c++ - QFont 到 qss 样式表

c++ - QSerialPort 5.2 中的 SerialPortError?