Tracking a variable during debugging



  • Hi everyone. How can I track a variable during debugging from the start? For example if the variable appears in 300 points, is there a smarter way than just put manually 300 breakpoints? Thank you.



  • Hi @Konstantinos ,

    You can use qDebug(). to track the variable or some console output.



  • Hi @Ni.Sumi

    Thank you, but can you be a little more specific? For example if I have the public variable "QString country" in one.h header, and then this variable appears in many .cpp files, 300 times, how can I track it anywhere? Where and how I will put the qDebug, and will I put breakpoint somewhere? Thanks again.



  • Hi @Konstantinos ,

    It does not matter how many times you to have to track and number of files. qDebug() is kind of std::cout. Wherever you want to track the variable or the value of the variable , there simply apply the qDebug().
    For example:

    in someCPPFile.cpp     
    //For your case QString country;
    #include <QDebug>
    qDebug()<< "Country Place1" << country;
    //some code
    qDebug()<< "Country Place2" << country;
    //some code
    qDebug()<< "Country Place3" << country;
    

    And you can see all the debug values in the console Output.



  • Hi,

    As @Ni-Sumi said u can use qDebug() , to track.
    Take a count and print in qDebug() the count along with the QString name and then u will come to how many times the
    For ex: QString value is printed.



  • @Ni.Sumi said in Tracking a variable during debugging:

    For example:

    in someCPPFile.cpp     
    //For your case QString country;
    #include <QDebug>
    qDebug()<< "Country Place1" << country;
    //some code
    qDebug()<< "Country Place2" << country;
    //some code
    qDebug()<< "Country Place3" << country;
    

    And you can see all the debug values in the console Output.

    Along with the qDebug() statements u can also add integer value to have a count and make the integer value static.

    Similar to the below code.

    in someCPPFile.cpp
    //For your case QString country;
    #include <QDebug>
    qDebug()<< "Country Place1" << country << value << endl;
    //some code
    qDebug()<< "Country Place2" << country << value << endl;
    //some code
    qDebug()<< "Country Place3" << country << value << endl;

    
    

    Thanks,



  • Thank you both for your answers, but maybe I was not so much specific about what exactly I want.

    During debugging, I like the way as the Qt Creator works. For example I like very much the Locals and Expressions tab (name, type,value), the debugger log etc, but I don't like very much the debugger console.

    So after clicking the debugging mode to start, is there a smart way to track a variable? For example can the debugging stop anytime that the variable changes value? Or anytime that it is used somewhere? Or anytime that debugger meets it?

    Simple said, I don't want to touch the code with writing many times qDebug()<<...

    I want with a smart way, just with my mouse choices to say to debugger to track the variable. If I go to the definition of the variable in the header, and put a custom breakpoint, it may help?

    Thanks again, I hope you understood me now.



  • I use VS and there you can add a watch to a variable to see it changing in the code and even set a watch on a piece of memory to trigger a breakpoint every time it changes. looks like Qt creator has something similar, see here: http://stackoverflow.com/questions/24786421/does-qt-have-a-watch-variable-debugging-function



  • Thank you VRonin. This is exactly what I wanted.

    But I don't know how to find out the address of the memory block that the variable uses - will use. Is there a way to find it out, especially before starting of debugging? Also, is it possible to force the variable to use a specific free address of the memory block?



  • You can't specify it before starting the debug nor force it to use a certain memory block. just put a breakpoint on the first time the variable is used (when its constructor is called?) and read the address from the "locals" window



  • I got it, really thank you for your help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.