[Qt-interest] Using qDebug with a QBitArray and QDataStream

Rohan Prabhu rohan at rohanprabhu.com
Wed Feb 2 13:50:04 CET 2011


For convenience, you could do:

QDebug operator<<(QDebug dbg, cons QBitArray& array) {
    QString text;
    for (int i = 0; i < z.size(); ++i)
        text += z.testBit(i) ? "1": "0";
    dbg << text;
    return dbg;
}

and then use:

QBitArray b;
qDebug()<<b;

anywhere in your code.

Regards,
rohan

On Wed, Feb 2, 2011 at 1:20 PM, Thiago Macieira <thiago at kde.org> wrote:
> On Wednesday, 2 de February de 2011 08:41:29 Alex Strickland wrote:
>> Hi
>>
>> Can someone tell me why this doesn't work?:
>>
>>          QByteArray debugByteArray;
>>          QDataStream debugStream( &debugByteArray, QIODevice::ReadWrite);
>>          QBitArray z( 10 );
>>          z.setBit( 3, true );
>>          debugStream << z;
>>          qDebug() << debugByteArray;
>>
>> or should I say doesn't do what I expect, the output is:
>>
>> "
>
> Right, it does work, it just doesn't output what you expected. You're trying
> to see the debug output of a binary byte array. What happens here is that
> there are embedded NULs inside that array and qDebug got confused.
>
> qDebug is not meant for binary data. It will result in an incomplete output if
> it gets one.
>
>>
>> I know I can do this:
>>
>>       QString text;
>>       for (int i = 0; i < z.size(); ++i)
>>               text += z.testBit(i) ? "1": "0";
>>       qDebug() << text;
>>
>> so this more about my education than solving my problem!
>
> Here you're sending text to the debugging output. This is expected to work.
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
>  Senior Product Manager - Nokia, Qt Development Frameworks
>      PGP/GPG: 0x6EF45358; fingerprint:
>      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>



More information about the Qt-interest-old mailing list