Visual Studio 2015 does not display QString contents during debug / debugging [duplicate]

1

I'm using Visual Studio 2015 to develop an application in Qt 5. Everything works correctly, but when I try to debug the code and inspect a variable of type QString , the debug does not show the contents (text) of the variable, but yes internal information of the object's allocation (both in the tooltip when I hover over the variable, like in the Watch window):

Is there any way to make VS display the textual content of the variable, just as it does with ANSI and STL strings?

    
asked by anonymous 18.08.2016 / 15:35

1 answer

0

Visual Studio has a feature called Custom View of Native Objects (a free translation of original in English Custom Views of Native Objects ). On the Microsoft blog you have a nice example .

  

The natvis file should be added to the project, for private use,   or the C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Packages\Debugger\Visualizers folder for general use.

Until sometime there was the Qt Visual Studio Add-in , which when installed provided a number of facilities for the use of Qt in VS, including a natvis file . This add-in is now deprecated but you can use your qt5.natvis file (found in some repositories on the Internet, such as esse ). Part of the file is reproduced below, but download it from the original source (it is too large to fit all here):

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="QPoint">
        <AlternativeType Name="QPointF"/>
        <DisplayString>{{ x = {xp}, y = {yp} }}</DisplayString>
        <Expand>
            <Item Name="[x]">xp</Item>
            <Item Name="[y]">yp</Item>
        </Expand>
    </Type>
    <Type Name="QRect">
        <DisplayString>{{ x = {x1}, y = {y1}, width = {x2 - x1 + 1}, height = {y2 - y1 + 1} }}</DisplayString>
        <Expand>
            <Item Name="[x]">x1</Item>
            <Item Name="[y]">y1</Item>
            <Item Name="[width]">x2 - x1 + 1</Item>
            <Item Name="[height]">y2 - y1 + 1</Item>
        </Expand>
    </Type>
    <Type Name="QRectF">
        <DisplayString>{{ x = {xp}, y = {yp}, width = {w}, height = {h} }}</DisplayString>
        <Expand>
            <Item Name="[x]">xp</Item>
            <Item Name="[y]">yp</Item>
            <Item Name="[width]">w</Item>
            <Item Name="[height]">h</Item>
        </Expand>
    </Type>
    <Type Name="QSize">
        <AlternativeType Name="QSizeF"/>
        <DisplayString>{{ width = {wd}, height = {ht} }}</DisplayString>
        <Expand>
            <Item Name="[width]">wd</Item>
            <Item Name="[height]">ht</Item>
        </Expand>
    </Type>
    <Type Name="QLine">
        <AlternativeType Name="QLineF"/>
        <DisplayString>{{ start point = {pt1}, end point = {pt2} }}</DisplayString>
        <Expand>
            <Synthetic Name="[start point]">
                <DisplayString>{pt1}</DisplayString>
                <Expand>
                    <ExpandedItem>pt1</ExpandedItem>
                </Expand>
            </Synthetic>
            <Synthetic Name="[end point]">
                <DisplayString>{pt2}</DisplayString>
                <Expand>
                    <ExpandedItem>pt2</ExpandedItem>
                </Expand>
            </Synthetic>
        </Expand>
    </Type>

    . . .

    <Type Name="QChar">
        <DisplayString>{ucs,c}</DisplayString>
        <StringView>ucs,c</StringView>
        <Expand>
            <Item Name="[latin 1]">ucs > 0xff ? '
<Type Name="QString">
    <DisplayString IncludeView="nq">{((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),sub}</DisplayString>
    <DisplayString ExcludeView="nq">"{((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),sub}"</DisplayString>
    <StringView>((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),sub</StringView>
    <Expand HideRawView="true">
        <Item ExcludeView="simple" Name="[size]">d-&gt;size</Item>
        <Item ExcludeView="simple" Name="[referenced]">d-&gt;ref.atomic._q_value</Item>
        <ArrayItems ExcludeView="simple">
            <Size>d-&gt;size</Size>
            <ValuePointer>((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),c</ValuePointer>
        </ArrayItems>
    </Expand>
</Type>
' : char(ucs),c</Item> <Item Name="[unicode]">ucs,c</Item> </Expand> </Type> <Type Name="QString"> <DisplayString>{d-&gt;data,sub}</DisplayString> <StringView>d-&gt;data,sub</StringView> <Expand> <Item Name="[size]">d-&gt;size</Item> <Item Name="[referenced]">d-&gt;ref._q_value</Item> <ArrayItems> <Size>d-&gt;size</Size> <ValuePointer>(d->data),c</ValuePointer> </ArrayItems> </Expand> </Type> <Type Name="QT::QString"> <DisplayString>{d-&gt;data,sub}</DisplayString> <StringView>d-&gt;data,sub</StringView> <Expand> <Item Name="[size]">d-&gt;size</Item> <Item Name="[referenced]">d-&gt;ref._q_value</Item> <ArrayItems> <Size>d-&gt;size</Size> <ValuePointer>(d->data),c</ValuePointer> </ArrayItems> </Expand> </Type> . . . </AutoVisualizer>

The QString view of this file does not work in Qt version 5.7, but there is an update that can be replaced ( the source is from SO ):

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="QPoint">
        <AlternativeType Name="QPointF"/>
        <DisplayString>{{ x = {xp}, y = {yp} }}</DisplayString>
        <Expand>
            <Item Name="[x]">xp</Item>
            <Item Name="[y]">yp</Item>
        </Expand>
    </Type>
    <Type Name="QRect">
        <DisplayString>{{ x = {x1}, y = {y1}, width = {x2 - x1 + 1}, height = {y2 - y1 + 1} }}</DisplayString>
        <Expand>
            <Item Name="[x]">x1</Item>
            <Item Name="[y]">y1</Item>
            <Item Name="[width]">x2 - x1 + 1</Item>
            <Item Name="[height]">y2 - y1 + 1</Item>
        </Expand>
    </Type>
    <Type Name="QRectF">
        <DisplayString>{{ x = {xp}, y = {yp}, width = {w}, height = {h} }}</DisplayString>
        <Expand>
            <Item Name="[x]">xp</Item>
            <Item Name="[y]">yp</Item>
            <Item Name="[width]">w</Item>
            <Item Name="[height]">h</Item>
        </Expand>
    </Type>
    <Type Name="QSize">
        <AlternativeType Name="QSizeF"/>
        <DisplayString>{{ width = {wd}, height = {ht} }}</DisplayString>
        <Expand>
            <Item Name="[width]">wd</Item>
            <Item Name="[height]">ht</Item>
        </Expand>
    </Type>
    <Type Name="QLine">
        <AlternativeType Name="QLineF"/>
        <DisplayString>{{ start point = {pt1}, end point = {pt2} }}</DisplayString>
        <Expand>
            <Synthetic Name="[start point]">
                <DisplayString>{pt1}</DisplayString>
                <Expand>
                    <ExpandedItem>pt1</ExpandedItem>
                </Expand>
            </Synthetic>
            <Synthetic Name="[end point]">
                <DisplayString>{pt2}</DisplayString>
                <Expand>
                    <ExpandedItem>pt2</ExpandedItem>
                </Expand>
            </Synthetic>
        </Expand>
    </Type>

    . . .

    <Type Name="QChar">
        <DisplayString>{ucs,c}</DisplayString>
        <StringView>ucs,c</StringView>
        <Expand>
            <Item Name="[latin 1]">ucs > 0xff ? '
<Type Name="QString">
    <DisplayString IncludeView="nq">{((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),sub}</DisplayString>
    <DisplayString ExcludeView="nq">"{((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),sub}"</DisplayString>
    <StringView>((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),sub</StringView>
    <Expand HideRawView="true">
        <Item ExcludeView="simple" Name="[size]">d-&gt;size</Item>
        <Item ExcludeView="simple" Name="[referenced]">d-&gt;ref.atomic._q_value</Item>
        <ArrayItems ExcludeView="simple">
            <Size>d-&gt;size</Size>
            <ValuePointer>((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),c</ValuePointer>
        </ArrayItems>
    </Expand>
</Type>
' : char(ucs),c</Item> <Item Name="[unicode]">ucs,c</Item> </Expand> </Type> <Type Name="QString"> <DisplayString>{d-&gt;data,sub}</DisplayString> <StringView>d-&gt;data,sub</StringView> <Expand> <Item Name="[size]">d-&gt;size</Item> <Item Name="[referenced]">d-&gt;ref._q_value</Item> <ArrayItems> <Size>d-&gt;size</Size> <ValuePointer>(d->data),c</ValuePointer> </ArrayItems> </Expand> </Type> <Type Name="QT::QString"> <DisplayString>{d-&gt;data,sub}</DisplayString> <StringView>d-&gt;data,sub</StringView> <Expand> <Item Name="[size]">d-&gt;size</Item> <Item Name="[referenced]">d-&gt;ref._q_value</Item> <ArrayItems> <Size>d-&gt;size</Size> <ValuePointer>(d->data),c</ValuePointer> </ArrayItems> </Expand> </Type> . . . </AutoVisualizer>

After saving the natvis file in the viewers directory and restarting VS, you have an appropriate view of QString :

    
30.06.2017 / 04:13