PDA

View Full Version : How to print variables with qDebug?



ricardo
12th July 2009, 15:50
Hi friends!

Usually I use qDebug to print variables values, but I guess it must be an easy way. I use 2 lines, QString and toAscii method. Is there any shorter way to do this?

QString m=QString("x %1, y %2, w %3, h %4 ").arg(selection_center_x).arg(selection_center_y). arg(selection_width).arg(selection_height);
qDebug(m.toAscii());

Thanks.

rexi
12th July 2009, 16:06
After including the QtDebug header, you can do something like



qDebug() << "x" << selection_center_x << "y" << selection_center_y << "w" << selection_width << "h" << selection_height;

gsmiko
12th July 2009, 16:12
Hi!

Try the stream operators! It's definitely shorter, but the code in the multi line approach is more manageable, it's easier to make small changes, or to reorganize the way variables are printed.

qDebug() << "x" << selection_center_x << ",y" << selection_center_y << ",w" << selection_width << ",h" << selection_height;

Edit: Oops! Sorry rexi! It looks like you finished writing your post before me. :o

ricardo
12th July 2009, 16:45
Thanks.

I tried that but it does not work. Should I include any header?



1>.\CUtil.cpp(60) : error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'QDebug' (or there is no acceptable conversion)
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qchar.h(389): could be 'QDataStream &operator <<(QDataStream &,const QChar &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qbytearray.h(569): or 'QDataStream &operator <<(QDataStream &,const QByteArray &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qstring.h(1062): or 'QDataStream &operator <<(QDataStream &,const QString &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/kernel/qobject.h(471): or 'QDebug operator <<(QDebug,const QObject *)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/kernel/qcoreapplication.h(272): or 'QDebug operator <<(QDebug,const MSG &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qpoint.h(103): or 'QDataStream &operator <<(QDataStream &,const QPoint &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qpoint.h(181): or 'QDebug operator <<(QDebug,const QPoint &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qpoint.h(235): or 'QDataStream &operator <<(QDataStream &,const QPointF &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qpoint.h(354): or 'QDebug operator <<(QDebug,const QPointF &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qsize.h(101): or 'QDataStream &operator <<(QDataStream &,const QSize &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qsize.h(196): or 'QDebug operator <<(QDebug,const QSize &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qsize.h(252): or 'QDataStream &operator <<(QDataStream &,const QSizeF &)'
1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qsize.h(357): or 'QDebug operator <<(QDebug,const QSizeF &)'
1> c:\qt\2009.02\qt\include\qtgui\../../src/gui/kernel/qcursor.h(151): or 'QDataStream &operator <<(QDataStream &,const QCursor &)'

gsmiko
12th July 2009, 17:03
#include <QDebug> should be enough.

drhex
12th July 2009, 18:20
qDebug("x %d, y %d, w %d, h %d", selection_center_x, selection_center_y, selection_width, selection_height);

should work with no extra headers

ricardo
12th July 2009, 21:08
No idea what is going on, but if I add #include <QDebug> it works. If I remove it, it odes not work. However qDebug("dsadsads"); works without #include <QDebug>.

Thanks.

wysota
13th July 2009, 00:27
If you're wondering why it happens, take a look at contents of the QtDebug header file (or rather the file it includes).