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

Asked

Viewed 85 times

2

I have a program in Qt 5 I use QString to manipulate text strings. However, when I debug the program, I cannot 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. The Visual Studio debugger (I use version 2015) displays the content in memory of the object QString instead of text directly (as it did in previous versions of Qt or VS):

inserir a descrição da imagem aqui

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

1 answer

2


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

What happens is that when the Qty Visual Studio Add-in, it installs a custom viewer into your VS allowing the QStrings are easily seen at debugging time.

If for some reason you can’t install Qt VS Add-in (if your VS is the version Express, for example), you can install the viewer manually. Do so:

  1. Go to the Add-in repository on Github, browse and download the file with extension .natvis the version of Qt you use. For example, this is version 5 file qt.
  2. Copy this file to the folder <MyDocuments>\Visual Studio <versão>\Visualizers. In the case of Visual Studio 2015 that I have here, for example, is the folder C:\Users\Luiz\Documents\Visual Studio 2015\Visualizers. It probably already exists, but if it doesn’t exist, it can create it.
  3. Restart Visual Studio.

After restarting, the strings using QString will be easily seen in the debugger:

inserir a descrição da imagem aqui

Note that the file .natvis also contains viewers for other Qt objects. It is too big to fully reproduce here, but just for reference, it has this format in version 5:

<?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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.