[solved] What is best practise of qDebug?

  • Hi,

    I'm new to Qt and still learning.

    I've found that we can use qDebug as follows.

    @qDebug ("Print me");
    qDebug () << "Print me";

    both statement generates same results.

    Are there any difference between these two?
    And what is better way?

    Thanks in advance.

  • The latter is more powerful than the former, as qDebug() return a QDebug object.

  • To just expand on
    [quote author="1+1=2" date="1375953327"]The latter is more powerful than the former, as qDebug() return a QDebug object.[/quote]

    With the second you can debug many different objects and structures without doing conversions.

    Say for instance you want to debug a number. With option 1 you need to do either one of the following:
    int myNumber = 10;
    qDebug("Print me: " + QString::number(myNumber));
    // or
    QString debugString = QString("Print me: %1").arg(myNumber);

    But using option 2 it is very straight forward:

    int myNumber = 10;
    qDebug() << "Print me: " << myNumber

    Most objects in Qt has default formats for qDebug that you do not need to convert, you can just << them to qDebug().

  • Hmm, to add to the above, the compiler will call the best conversion function to use in the << way. Otherwise you are responsible to do it your self to convert to QString.

  • 1+1=2, Badger, Jeroentje@ho..

    Thank you very much for your clear explanations.

    So I should use
    @qDebug () << "Print me";@

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.