Why do strings (QString) not appear during debugging in Visual Studio?

2

I have a program in Qt 5 where I use QString to manipulate text strings. However, when I debug the program, I can not see the contents of the strings (either in the "Auto", "Locals" or "Watch" window, or even when I leave the mouse pointer over the variable. I use the 2015 version) displays the in-memory contents of the QString object rather than the text directly (as I did in earlier versions of Qt or VS):

How do I get the debugger to correctly display string text?

    
asked by anonymous 14.03.2016 / 12:25

1 answer

2

This is the default behavior of Visual Studio in any version. Since it does not know how to display the QString object, it presents its internal contents as best it can.

What happens is that when you install the Qt Visual Studio Add-in , it installs a custom viewer on the your VS allowing QStrings to be easily seen in debug time.

If for some reason you can not install the Qt VS Add-in (if your VS is the Express version, for example), you can install the viewer manually. Here's how:

  • Go to the Github add-in repository , browse to and download the file with .natvis of the Qt version it uses. For example, this is the version 5 file of Qt .
  • Copy this file to the <MyDocuments>\Visual Studio <versão>\Visualizers folder. In the case of Visual Studio 2015 that I have here, for example, is the C:\Users\Luiz\Documents\Visual Studio 2015\Visualizers folder. It probably already exists, but if it does not exist you can create it.
  • Restart Visual Studio.
  • After restart, strings using QString will be easily seen in the debugger:

    Notethatthe.natvisfilealsocontainsviewersforotherQtobjects.Itistoolargetoreproducecompletelyhere,butforreferenceonly,ithasthisformatinversion5:

    <?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="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="QString"> <DisplayString>{((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> <Item Name="[size]">d-&gt;size</Item> <Item Name="[referenced]">d-&gt;ref.atomic._q_value</Item> <ArrayItems> <Size>d-&gt;size</Size> <ValuePointer>((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),c</ValuePointer> </ArrayItems> </Expand> </Type> . . . </AutoVisualizer>

    To learn more about native Visual Studio viewers, see the documentation .

        
    14.03.2016 / 12:28