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. how to add QDebug to inherited classes?
QtWS25 Last Chance

how to add QDebug to inherited classes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdebuginheritance
7 Posts 3 Posters 1.5k 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
    Dariusz
    wrote on last edited by Dariusz
    #1

    Hey

    Solution below thanks to > @Christian-Ehrlicher

    Kinda lost here...

    Say I have:

    class base {
        QString mBase;
    public:
        base() {};
        ~base() {};
        friend QDebug operator<<(QDebug &stream, const base &obj);
    };
    QDebug operator<<(QDebug &stream, const base &obj) {
        stream << obj.mBase;
        return stream;
    }
    
    class inh : public base {
        QString mInh;
    public:
        inh() {};
        ~inh() {};
        friend QDebug operator<<(QDebug &stream, const inh &obj);
    };
    QDebug operator<<(QDebug &stream, const inh &obj) {
        stream << obj.mInh;//"How do I call debug on base class here ?"
        return stream;
    }
    

    How to properly get inherited debug ?

    Ideally, I'd like something like this : - unless that's stupid and I missed elephant?

    QDebug operator<<(QDebug &stream, const inh &obj) {
        stream << obj.baseClassDebug;
        stream << obj::base; // or ?
        stream stream << obj.mInh;
        return stream;
    }
    

    TIA


    Working example :

    
    class base {
        QString mBase;
    public:
        base() : mBase("BASE CLASS") {};
        ~base() {};
        friend QDebug operator<<(QDebug &stream, const base &obj);
    };
    QDebug operator<<(QDebug &stream, const base &obj) {
        stream << obj.mBase;
        return stream;
    }
    
    class other {
        QString mBase;
    public:
        other() : mBase("SomeOtherClass") {};
        ~other() {};
        friend QDebug operator<<(QDebug &stream, const other &obj);
    };
    QDebug operator<<(QDebug &stream, const other &obj) {
        stream << obj.mBase;
        return stream;
    }
    
    
    class inh : public base {
        QString mInh;
    public:
        inh() : mInh("SINGLE INHERITED") {};
        ~inh() {};
        friend QDebug operator<<(QDebug &stream, const inh &obj);
    };
    QDebug operator<<(QDebug &stream, const inh &obj) {
        const base &b = obj;
        stream << b;
        stream << obj.mInh;
        return stream;
    }
    
    class dual : public base, public other {
        QString mInh;
    public:
        dual() : mInh("DUAL INHERITED") {};
        ~dual() {};
        friend QDebug operator<<(QDebug &stream, const dual &obj);
    };
    QDebug operator<<(QDebug &stream, const dual &obj) {
        const base &b = obj;
        const other &o = obj;
        stream << b;
        stream << o;
        stream << obj.mInh;
        return stream;
    }
    
    
        inh h;
        qDebug() << h;
        dual d;
        qDebug() << d;
    
    
    JonBJ 1 Reply Last reply
    0
    • D Dariusz

      Hey

      Solution below thanks to > @Christian-Ehrlicher

      Kinda lost here...

      Say I have:

      class base {
          QString mBase;
      public:
          base() {};
          ~base() {};
          friend QDebug operator<<(QDebug &stream, const base &obj);
      };
      QDebug operator<<(QDebug &stream, const base &obj) {
          stream << obj.mBase;
          return stream;
      }
      
      class inh : public base {
          QString mInh;
      public:
          inh() {};
          ~inh() {};
          friend QDebug operator<<(QDebug &stream, const inh &obj);
      };
      QDebug operator<<(QDebug &stream, const inh &obj) {
          stream << obj.mInh;//"How do I call debug on base class here ?"
          return stream;
      }
      

      How to properly get inherited debug ?

      Ideally, I'd like something like this : - unless that's stupid and I missed elephant?

      QDebug operator<<(QDebug &stream, const inh &obj) {
          stream << obj.baseClassDebug;
          stream << obj::base; // or ?
          stream stream << obj.mInh;
          return stream;
      }
      

      TIA


      Working example :

      
      class base {
          QString mBase;
      public:
          base() : mBase("BASE CLASS") {};
          ~base() {};
          friend QDebug operator<<(QDebug &stream, const base &obj);
      };
      QDebug operator<<(QDebug &stream, const base &obj) {
          stream << obj.mBase;
          return stream;
      }
      
      class other {
          QString mBase;
      public:
          other() : mBase("SomeOtherClass") {};
          ~other() {};
          friend QDebug operator<<(QDebug &stream, const other &obj);
      };
      QDebug operator<<(QDebug &stream, const other &obj) {
          stream << obj.mBase;
          return stream;
      }
      
      
      class inh : public base {
          QString mInh;
      public:
          inh() : mInh("SINGLE INHERITED") {};
          ~inh() {};
          friend QDebug operator<<(QDebug &stream, const inh &obj);
      };
      QDebug operator<<(QDebug &stream, const inh &obj) {
          const base &b = obj;
          stream << b;
          stream << obj.mInh;
          return stream;
      }
      
      class dual : public base, public other {
          QString mInh;
      public:
          dual() : mInh("DUAL INHERITED") {};
          ~dual() {};
          friend QDebug operator<<(QDebug &stream, const dual &obj);
      };
      QDebug operator<<(QDebug &stream, const dual &obj) {
          const base &b = obj;
          const other &o = obj;
          stream << b;
          stream << o;
          stream << obj.mInh;
          return stream;
      }
      
      
          inh h;
          qDebug() << h;
          dual d;
          qDebug() << d;
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Dariusz
      I don't know, but does https://stackoverflow.com/questions/2677577/how-to-overload-operator-for-qdebug help you? It even has an answer there from our friend @raven-worx here, https://forum.qt.io/topic/34523/solved-overloading-qdebug-stream-operation !

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        Yeah I saw the QString option and other one. However it feels a bit "wrong"... That would mean that all types/specific stuff I would have to rewrite from existing QDebug overloads to QString overloads... Kinda "ok" to do but I thought there I a proper QDebug way of doing it ?

        This feels a bit "hacky"... like hey Use the QDebug, but don't use it for anything a little more complex? not sure...

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          This is a plain C++ problem

          QDebug operator<<(QDebug &stream, const inh &obj) {
              const base &b = obj;
              stream << b;
              stream << obj.mInh;
              return stream;
          }
          

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          D 1 Reply Last reply
          5
          • Christian EhrlicherC Christian Ehrlicher

            This is a plain C++ problem

            QDebug operator<<(QDebug &stream, const inh &obj) {
                const base &b = obj;
                stream << b;
                stream << obj.mInh;
                return stream;
            }
            
            D Offline
            D Offline
            Dariusz
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in how to add QDebug to inherited classes?:

            This is a plain C++ problem

            QDebug operator<<(QDebug &stream, const inh &obj) {
                const base &b = obj;
                stream << b;
                stream << obj.mInh;
                return stream;
            }
            

            This looks quite awesome!

            I've updated 1st post with multiple inheritances working example. Hope it helps in future.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dariusz
              wrote on last edited by Dariusz
              #6

              Ok I jumped the gun... - naturally...

              So the solution above works great... but what can I do about this example?

                  inh *h = new inh();
                  qDebug() << *h;
                  dual *d = new dual();
                  qDebug() << *d;
                  base *b = d;
                  qDebug() << *b;
              

              It prints :

              "BASE CLASS" "SINGLE INHERITED" 
              "BASE CLASS" "SomeOtherClass" "DUAL INHERITED" 
              "BASE CLASS" 
              

              it should print :

              "BASE CLASS" "SINGLE INHERITED" 
              "BASE CLASS" "SomeOtherClass" "DUAL INHERITED" 
              "BASE CLASS" "SomeOtherClass" "DUAL INHERITED" 
              

              Any ideas how to deal with this?

              The main issue I got is that

              QDebug overload<<(QDebug source,const base&obj) is a const class... if only we could do a
              QDebug overload<<(QDebug source,base&obj)
              then I could do obj.myDebug()
              where as virtual QDebug myDebug() could do :

              virtual QDebug myDebug(){
              QDebug s;
              s  << aseClass::myDebug()
              s<< "Extra pars";
              return s
              }
              

              If that is "possible"... but then again the main QDebug overload is const so I can't call inside class... mhmmm ?
              And it has to be const base&obj because else QVector<base*> won't work...

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Since you're explicitly calling QDebug operator<<(QDebug &stream, const base &obj) the compiler can't call the other function.
                What you can do is to write your own "virtual QString debug() const" function in every class which you the call within the debug stream operator function. But once again - plain C++ :)

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2

                • Login

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