How do I see the contents of Qt objects QByteArray during debugging?

aviit picture aviit · Aug 31, 2012 · Viewed 8.4k times · Source

My program use some variables of type QByteArray to contain data (bytes). That bytes maybe are special characters like '\0', 1, ... So I cannot see all elements after special character when debugging. If I use std::vector, I can see all elements. I must use QByteArray but I cannot see all element of this variable when debugging. Help me, plz! Thanks!

Example: QByteArray bytes(4, '\0'); Now, if debug, I just see "". But I want to see "'\0', '\0', '\0', '\0'" or something like like this.

I also have the same problem with QDateTime. But I resolved by this help: http://www.qtcentre.org/threads/32336-View-QDate-and-QDateTime-in-VisualStudio-debugger

This link may help but this not resolve my problem: http://qt-project.org/wiki/IDE-debug-helpers

Example:

QByteArray ba("Hello"); //debuging, see: ba = "Hello"     
ba.append('\0');     
ba.append("a message"); //we just see: ba = "Hello"   

Answer

yota picture yota · Oct 16, 2012

I contribute here a piece of code I had to write since I could not find any method doing something close to that: display a QByteArray as a meaningful QString in a way python would do it: ascii is kept as this, special char are displayed as hexadecimal code.

If someone knows a better way ! (here it's QT 4.6)

QString toDebug(const QByteArray & line) {

    QString s;
    uchar c;

    for ( int i=0 ; i < line.size() ; i++ ){
        c = line[i];
        if ( c >= 0x20 and c <= 126 ) {
            s.append(c);
        } else {
            s.append(QString("<%1>").arg(c, 2, 16, QChar('0')));
        }
    }
    return s;
}

Such as:

QByteArray a;
a.append("et");
a.append('\0');
a.append("voilà");
qDebug() << toDebug(QByteArray(a));

returns:

"et<00>voil<e0>"