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. toString() method equivalent for debugging
QtWS25 Last Chance

toString() method equivalent for debugging

Scheduled Pinned Locked Moved General and Desktop
debugclassstring
4 Posts 3 Posters 4.9k 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.
  • D Offline
    D Offline
    dridk
    wrote on 10 Mar 2015, 20:32 last edited by dridk 3 Oct 2015, 20:33
    #1

    Hi,

    I come back from other language country like Python and Java Island... Both of them have a toString feature , which allow debugging class.
    Is there something equivalent that I didn't see before ?
    As example, I want to do :

    MyClass car;
    qDebug()<<car;
    

    Nothing in Biology Makes Sense Except in the Light of Evolution

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Mar 2015, 20:48 last edited by
      #2

      Hi,

      For that kind of debugging you would usually implement a custom QDebug stream operator like described here

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dridk
        wrote on 10 Mar 2015, 20:53 last edited by
        #3

        yes, but this is ugly.... I have to implement it outside the class.. That's mean create a MyClass::toString() and call it inside the QDebug operator... So, I prefer to write :

         qDebug()<<myClass.toString();
        

        Nothing in Biology Makes Sense Except in the Light of Evolution

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 10 Mar 2015, 21:14 last edited by Chris Kawa 3 Oct 2015, 21:16
          #4

          From a design point of view it's not ugly. It's actually prettier.

          For example if you have a class Triangle or APieceOfRock they have conceptually no debugging or to string conversion notion whatsoever.
          Why should a triangle or a piece of rock know anything about strings or how to convert to them? Should it also convert to other data types like ACar or QWidget? Should you add a conversion method to them every time you introduce another type in your app? Making toSting() method in every class is pumping in it functionality that is out of their purpose scope and does not belong to that class.

          A separate utility function, detached from the class is actually a much cleaner solution that keeps responsibility separation.

          1 Reply Last reply
          1

          4/4

          10 Mar 2015, 21:14

          • Login

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