[Qt-interest] How to compress QString

Iurii Gordiienko hordi at ukr.net
Thu Apr 16 11:29:59 CEST 2009


Hi    Thank you    
    
--- Original Message ---    
From: <Oliver.Knoll at comit.ch>    
To: <qt-interest at trolltech.com>    
Date: 16 april, 12:08:47    
Subject: Re: [Qt-interest] How to compress QString    
    
    Iurii Gordiienko wrote on Thursday, April 16, 2009 10:36 AM:    
    
> Hi    
>    
> How to compress-uncompress the QString using the qCompress and    
> qUncompress if I need to support the unicode strings? Qt-4.5.0    
    
- Compress    
    
One flavour of qCompress takes an uchar * and the number of bytes as argument:    
    
QByteArray qCompress (const uchar *data, int nbytes, int compressionLevel = -1)    
    
You get access to the raw unicode bytes within a QString with several methods:    
    
http://doc.trolltech.com/4.5/qstring.html#data-2 (you get a QChar *)    
http://doc.trolltech.com/4.5/qstring.html#utf16 (you get a ushort *)    
http://doc.trolltech.com/4.5/qstring.html#unicode (you get a QChar *)    
    
The number of bytes should also be fairly simple to figure out, something like QString::size() * 2 or so. After some QChar->unicode() and some casting to uchar * you should be able to succesfully pass this data to qCompress.    
    
- Decompress    
qUncompress always returns a raw QByteArray. Given that data you should be able to put it as unicode into a QString:    
    
http://doc.trolltech.com/4.5/qstring.html#setUnicode    
http://doc.trolltech.com/4.5/qstring.html#setUtf16    
    
Again you need to figure out the 'size' (number of characters! not number of bytes this time) of the unicode contained in QByteArray and pass this to either method above as well. And you need to think a bit about how you figure out whether the uncompressed QByteArray really contains unicode:    
    
- You explicitly encode this in your compressed stream or    
- "You simply know"    
    
Cheers, Oliver    
    
--    
Oliver Knoll    
Dipl. Informatik-Ing. ETH    
COMIT AG - ++41 79 520 95 22    
    
_______________________________________________    
Qt-interest mailing list    
Qt-interest at trolltech.com    
http://lists.trolltech.com/mailman/listinfo/qt-interest    
    
    
    
    
--    
    Рецепт успеха в Сети. Новый номер журнала InternetUA.  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090416/6e68e959/attachment.html 


More information about the Qt-interest-old mailing list