Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Tracking a variable during debugging
QtWS25 Last Chance

Tracking a variable during debugging

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • KonstantinosK Offline
    KonstantinosK Offline
    Konstantinos
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Ni.SumiN Offline
      Ni.SumiN Offline
      Ni.Sumi
      wrote on last edited by
      #2

      Hi @Konstantinos ,

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

      KonstantinosK 1 Reply Last reply
      2
      • Ni.SumiN Ni.Sumi

        Hi @Konstantinos ,

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

        KonstantinosK Offline
        KonstantinosK Offline
        Konstantinos
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Ni.SumiN Offline
          Ni.SumiN Offline
          Ni.Sumi
          wrote on last edited by Ni.Sumi
          #4

          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.

          1 Reply Last reply
          2
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            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.

            Pradeep Kumar
            Qt,QML Developer

            1 Reply Last reply
            1
            • Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by
              #6

              @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,

              Pradeep Kumar
              Qt,QML Developer

              1 Reply Last reply
              2
              • KonstantinosK Offline
                KonstantinosK Offline
                Konstantinos
                wrote on last edited by Konstantinos
                #7

                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.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  4
                  • KonstantinosK Offline
                    KonstantinosK Offline
                    Konstantinos
                    wrote on last edited by Konstantinos
                    #9

                    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?

                    1 Reply Last reply
                    1
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      3
                      • KonstantinosK Offline
                        KonstantinosK Offline
                        Konstantinos
                        wrote on last edited by
                        #11

                        I got it, really thank you for your help.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved