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