[Qt-interest] Convert from a QPixmap into a QByteArray, and back

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Wed May 19 02:04:33 CEST 2010


Gabriele,
 
You are using incompatible methods to write and read the image.  Did you see the warning on QPixmap QDataStream operator <<?  
 
You can use operator >> to read the byte array.  
 
Or add a QBuffer around the QByteArray and use QPixmap load/save to QIODevice.  
 
Tony.
 

-----Original Message-----

Hello,

In the process of debugging I've found myself unable to assert the following code:


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    const QPixmap screen = QPixmap::grabWindow(QApplication::desktop()->winId());
    QByteArray *bytes = new QByteArray();
    QDataStream out(bytes, QIODevice::WriteOnly);
    out << screen;

    QPixmap* screen1 = new QPixmap();
    screen1->loadFromData(bytes->data());

    std::cout << screen1->save("7777.png", "PNG") << endl;
    std::cout << screen.save("6666.png", "PNG") << endl;

    return a.exec();
}

All I'm doing is trying to convert from a QPixmap into a QByteArray, and back. Although the code is mostly verbatim from QPixmap and QDataStream, it doens't work. Indeed screen1 fails to save into 7777.png.

Your help, fixing this code, is greatly appreciated.

-- 
Regards, 
K. Gabriele

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100519/b81e2d36/attachment.html 


More information about the Qt-interest-old mailing list