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. [SOLVED] Overloading qDebug << stream operation

[SOLVED] Overloading qDebug << stream operation

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 9.6k 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.
  • X Offline
    X Offline
    xcoder
    wrote on 20 Nov 2013, 16:13 last edited by
    #1

    Hello everyone,

    I've been stuck on getting qDebug<< stream operator to work with my class which extends QGraphicsRectItem.

    This is what I have as of know in my header file:

    @
    class blockItem : public QGraphicsRectItem
    {
    public:
    enum { Type = UserType + 15 };

    blockItem();
    blockItem(const blockItem &node);
    ~blockItem();
    
    friend QDebug operator<<(QDebug dbg, const blockItem &c);
    

    ...
    };

    Q_DECLARE_METATYPE(blockItem)

    inline QDebug operator<<(QDebug dbg, const blockItem &c);
    @

    And my Cpp file:

    @
    inline QDebug operator<<(QDebug dbg, const blockItem &c)
    {
    dbg.nospace() << "blockItem(" << c.getMyId() << ", " << c.getMyType() << ", " << c.pos() << ")";

    return dbg.space();
    

    }
    @

    I tried many different ways I could find online, making the dbg as a reference, with and without inline tag. With friend tag and without.
    Still no luck. The code compiles, without any warnings or errors, but the problem is when I try to pass the blockItem class to the qDebug, it writes it as QGRaphicsItem not as my blockItem.

    Basically the output of qDebug is like this:
    @
    QGraphicsItem(this =0x32d25c8, parent =0x0, pos =QPointF(0, 0) , z = 1 , flags = ( ItemIsMovable | ItemIsSelectable | ItemSendsGeometryChanges ) )
    @

    However I'm expecting something like this:
    @
    blockItem(152, StandardNode, QPointF(0, 0))
    @

    Any ideas, what I might be missing? How come QGraphicsItem overloads my custom qDebug() stream operator? Is it even possible to overload qDebug() stream operator if I'm extending the QGraphicsItem class?

    Kind Regards
    Raivis

    Only a biker knows why a dog sticks his head out of a car window.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 20 Nov 2013, 16:53 last edited by
      #2

      The following is a correct implementation of the debug streaming operator. I don't see any big differences to your implementation (except superfluous the friend and inline keyword).

      h (not a member, but in global scope):
      @
      QDebug operator<<(QDebug dbg, const MyType &type);
      @

      cpp:
      @
      QDebug operator<<(QDebug dbg, const MyType &type)
      {
      dbg.nospace() << "MyType(" << .... << ")";
      return dbg.maybeSpace();
      }
      @

      How do you print your item? Do you pass a variable of the type QGraphicsItem to the qdebug stream?
      If so the problem is, that global operators are not "virtual". So if you pass it a QGraphicsItem the streaming operators for QGraphicsItem are taken, even if the QgraphicsItem is your custom item derivation.
      So you would need to cast the item to your custom type first.

      See "this":http://stackoverflow.com/questions/4571611/making-operator-virtual.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • X Offline
        X Offline
        xcoder
        wrote on 26 Nov 2013, 13:57 last edited by
        #3

        Thanks raven-worx!

        You were correct I was just passing incorrectly the object, the overload itself was working.

        Basically I changed everything like you have, except I added another QDebug overload method:

        my header:
        @
        Q_DECLARE_METATYPE(blockItem)

        QDebug operator<<(QDebug dbg, const blockItem &c);
        QDebug operator<<(QDebug dbg, const blockItem *c);
        @

        and my cpp:
        @
        QDebug operator<<(QDebug dbg, const blockItem &c)
        {
        dbg.nospace() << "blockItem(" << c.getMyId() << ", " << c.getMyType() << ", " << c.pos() << ")";

        return dbg.space();
        

        }

        QDebug operator<<(QDebug dbg, const blockItem *c)
        {
        dbg.nospace() << *c;

        return dbg.space();
        

        }
        @

        I got the answer from "here":http://stackoverflow.com/questions/9840952/overloaded-stdostream-operator-not-called-stream-gets-variable-address-inst, if anybody is interested

        Only a biker knows why a dog sticks his head out of a car window.

        1 Reply Last reply
        0

        1/3

        20 Nov 2013, 16:13

        • Login

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