Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualize QUrl in debug as string in VS

Generally, I like QUrls, but its such a pain to debug code with a lot of QUrls in code, and I cant see the actual url string in debug, I have to put some debug calls to toString in code. Is there any possibility to make it visible in debug watch?

like image 463
mishander Avatar asked Jan 23 '26 11:01

mishander


2 Answers

I know this is old, but I've just stumbled upon this problem myself and decided to solve it somehow. The solution is crude, to say the least, but it works.

The problem with QUrl is that is uses pimpl for all of its internals and you don't have access to the definition of QUrlPrivate while debugging. The solution here might break once anything in QUrl is changed, as it is based on offsets of QUrlPrivate members. So if anything breaks in future versions, you could adjust offsets and you're good. This is as of 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>

Just save it into (for instance):

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

Hope it's useful to someone.

EDIT:

This assumes you have natvis for other Qt types installed, especially QString.

like image 178
W.B. Avatar answered Jan 25 '26 02:01

W.B.


There is update of @W.B. answer for 64 bit version Visual Studio 2017 and Qt 5.12.9. Only offsets were changed.

<?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)
+ 4+4)->d->size">{*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d)
+ 4+4)}://{*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d)
+ 4*2+8*3)}{*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d)
+ 4*2+8*4)}</DisplayString>
        <DisplayString>{*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d)
+ 4*2+8*4)}</DisplayString>
        <Expand>
            <Item Name="[scheme]">*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d)
+ 4+4)</Item>
            <Item Name="[host]">*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d)
+ 4*2+8*3)</Item>
            <Item Name="[path]">*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d)
+ 4*2+8*4)</Item>
            <Item Name="[query]">*reinterpret_cast&lt;QString*&gt;(reinterpret_cast&lt;char*&gt;(d)
+ 4*2+8*5)</Item>
        </Expand>
    </Type>


</AutoVisualizer>
like image 45
Marat Shamsetdinov Avatar answered Jan 25 '26 00:01

Marat Shamsetdinov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!