Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Overloading QDebug in QtObject subclass
Forum Updated to NodeBB v4.3 + New Features

Overloading QDebug in QtObject subclass

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 221 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.
  • T Offline
    T Offline
    Tobias Fensch
    wrote on last edited by Tobias Fensch
    #1

    Hello,
    I want to debug out some information about a custom QtObject-subclass. Therefore I'm using the operator<< mechanism as described here.

    This doesn't work for me.

    My Test-Implementation:

    DebugClass.h

    #ifndef DEBUGCLASS_H
    #define DEBUGCLASS_H
    
    #include <QObject>
    
    class DebugClass : public QObject
    {
    	Q_OBJECT
    public:
    	explicit DebugClass(QObject *parent = nullptr);
    
    	int getX() const;
    
    signals:
    
    private:
    	int x;
    };
    
    QDebug operator<< (QDebug , const DebugClass &);
    
    #endif // DEBUGCLASS_H
    

    DebugClass.cpp

    #include "DebugClass.h"
    #include <QDebug>
    
    DebugClass::DebugClass(QObject *parent)
    	: QObject{parent}
    {
    	x = 42;
    }
    
    QDebug operator<<(QDebug dbg, const DebugClass &info)
    {
    	dbg.nospace() << "This is x: " << info.getX();
    	return dbg.maybeSpace();
    }
    
    int DebugClass::getX() const
    {
    	return x;
    }
    

    Using the DebugClass:

    auto debugClass = new DebugClass();
    qDebug() << "Test DebugClass:" << debugClass;
    

    This gives me the following output:

    "Test DebugClass: DebugClass(0x2af8d9e1250)"
    

    The <<-Operator is never called.
    Can someone help me with that?

    thanks,
    Tobias

    jsulmJ 1 Reply Last reply
    0
    • kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on last edited by
      #3

      @Tobias-Fensch said in Overloading QDebug in QtObject subclass:

      "Test DebugClass: DebugClass(0x2af8d9e1250)"

      The <<-Operator is never called.

      It's not called because you pipe the pointer to your type, but the operator<<() expects a value (const reference) of your type.

      To make C++ call your operator<<() make sure to de-reference the pointer.

      Director R&D, The Qt Company

      1 Reply Last reply
      2
      • T Tobias Fensch

        Hello,
        I want to debug out some information about a custom QtObject-subclass. Therefore I'm using the operator<< mechanism as described here.

        This doesn't work for me.

        My Test-Implementation:

        DebugClass.h

        #ifndef DEBUGCLASS_H
        #define DEBUGCLASS_H
        
        #include <QObject>
        
        class DebugClass : public QObject
        {
        	Q_OBJECT
        public:
        	explicit DebugClass(QObject *parent = nullptr);
        
        	int getX() const;
        
        signals:
        
        private:
        	int x;
        };
        
        QDebug operator<< (QDebug , const DebugClass &);
        
        #endif // DEBUGCLASS_H
        

        DebugClass.cpp

        #include "DebugClass.h"
        #include <QDebug>
        
        DebugClass::DebugClass(QObject *parent)
        	: QObject{parent}
        {
        	x = 42;
        }
        
        QDebug operator<<(QDebug dbg, const DebugClass &info)
        {
        	dbg.nospace() << "This is x: " << info.getX();
        	return dbg.maybeSpace();
        }
        
        int DebugClass::getX() const
        {
        	return x;
        }
        

        Using the DebugClass:

        auto debugClass = new DebugClass();
        qDebug() << "Test DebugClass:" << debugClass;
        

        This gives me the following output:

        "Test DebugClass: DebugClass(0x2af8d9e1250)"
        

        The <<-Operator is never called.
        Can someone help me with that?

        thanks,
        Tobias

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Tobias-Fensch Did you put operator<< into a header file and did you include this header file where you are trying to use operator<<?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • kkoehneK Offline
          kkoehneK Offline
          kkoehne
          Moderators
          wrote on last edited by
          #3

          @Tobias-Fensch said in Overloading QDebug in QtObject subclass:

          "Test DebugClass: DebugClass(0x2af8d9e1250)"

          The <<-Operator is never called.

          It's not called because you pipe the pointer to your type, but the operator<<() expects a value (const reference) of your type.

          To make C++ call your operator<<() make sure to de-reference the pointer.

          Director R&D, The Qt Company

          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