Results 1 to 8 of 8

Thread: How to print variables with qDebug?

  1. #1
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to print variables with qDebug?

    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.

  2. #2
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to print variables with qDebug?

    After including the QtDebug header, you can do something like

    Qt Code:
    1. qDebug() << "x" << selection_center_x << "y" << selection_center_y << "w" << selection_width << "h" << selection_height;
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to rexi for this useful post:

    ricardo (12th July 2009)

  4. #3
    Join Date
    Sep 2008
    Posts
    25
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to print variables with qDebug?

    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.
    Qt Code:
    1. qDebug() << "x" << selection_center_x << ",y" << selection_center_y << ",w" << selection_width << ",h" << selection_height;
    To copy to clipboard, switch view to plain text mode 

    Edit: Oops! Sorry rexi! It looks like you finished writing your post before me.
    Last edited by gsmiko; 12th July 2009 at 15:19.

  5. The following user says thank you to gsmiko for this useful post:

    ricardo (12th July 2009)

  6. #4
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to print variables with qDebug?

    Thanks.

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

    Qt Code:
    1. 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)
    2. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qchar.h(389): could be 'QDataStream &operator <<(QDataStream &,const QChar &)'
    3. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qbytearray.h(569): or 'QDataStream &operator <<(QDataStream &,const QByteArray &)'
    4. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qstring.h(1062): or 'QDataStream &operator <<(QDataStream &,const QString &)'
    5. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/kernel/qobject.h(471): or 'QDebug operator <<(QDebug,const QObject *)'
    6. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/kernel/qcoreapplication.h(272): or 'QDebug operator <<(QDebug,const MSG &)'
    7. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qpoint.h(103): or 'QDataStream &operator <<(QDataStream &,const QPoint &)'
    8. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qpoint.h(181): or 'QDebug operator <<(QDebug,const QPoint &)'
    9. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qpoint.h(235): or 'QDataStream &operator <<(QDataStream &,const QPointF &)'
    10. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qpoint.h(354): or 'QDebug operator <<(QDebug,const QPointF &)'
    11. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qsize.h(101): or 'QDataStream &operator <<(QDataStream &,const QSize &)'
    12. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qsize.h(196): or 'QDebug operator <<(QDebug,const QSize &)'
    13. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qsize.h(252): or 'QDataStream &operator <<(QDataStream &,const QSizeF &)'
    14. 1> c:\qt\2009.02\qt\include\qtcore\../../src/corelib/tools/qsize.h(357): or 'QDebug operator <<(QDebug,const QSizeF &)'
    15. 1> c:\qt\2009.02\qt\include\qtgui\../../src/gui/kernel/qcursor.h(151): or 'QDataStream &operator <<(QDataStream &,const QCursor &)'
    To copy to clipboard, switch view to plain text mode 

  7. #5
    Join Date
    Sep 2008
    Posts
    25
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to print variables with qDebug?

    #include <QDebug> should be enough.

  8. The following user says thank you to gsmiko for this useful post:

    ricardo (12th July 2009)

  9. #6
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to print variables with qDebug?

    Qt Code:
    1. qDebug("x %d, y %d, w %d, h %d", selection_center_x, selection_center_y, selection_width, selection_height);
    To copy to clipboard, switch view to plain text mode 

    should work with no extra headers

  10. The following 2 users say thank you to drhex for this useful post:

    ricardo (12th July 2009), umulingu (14th July 2009)

  11. #7
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to print variables with qDebug?

    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.

  12. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to print variables with qDebug?

    If you're wondering why it happens, take a look at contents of the QtDebug header file (or rather the file it includes).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. The following user says thank you to wysota for this useful post:

    umulingu (14th July 2009)

Similar Threads

  1. Trouble to print with QPrintPreviewWidget
    By estanisgeyer in forum Qt Programming
    Replies: 5
    Last Post: 24th December 2009, 06:14
  2. Crashing without qDebug() ?
    By xtreme in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2008, 17:01
  3. Change margin on QTextEdit print()
    By Dii in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2008, 22:45
  4. creating query string from variables
    By locus in forum General Programming
    Replies: 2
    Last Post: 16th April 2007, 08:50
  5. Disable qDebug output
    By the_bis in forum Qt Programming
    Replies: 7
    Last Post: 30th November 2006, 20:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.