c++qtqimageqbytearrayqdatastream

Converting QImage to QByteArray using QDataStream


Im trying to convert a QImage that maked from ScreenShot to a QByteArray for sending via QTCPSocket. when i convert QImage to QByteArray and before sending it i try to deserialize and show it on label it cant ! what's my mistake? thx for helping.

QByteArray ImClientShooter::toQByteArray(QImage &img)
{
    QByteArray temp;
    QDataStream data(&temp, QIODevice::ReadWrite);
    data « img;
    return temp;
}
QByteArray goOn{toQByteArray(sampleQImage)};     //sampleQImage is a QImage Object
lbl->setPixmap(QPixmap::fromImage( (QImage::fromData(goOn))));  // QLabel* lbl

sampleQImage maked from ScreenShot:

QGuiApplication::primaryScreen()->grabWindow(0).toImage();

Solution

  • mission passed :D

    sender:

    QImage img;
    QByteArray *ba;
    bool flag{1};
    QDataStream out(ba,QIODevice::ReadWrite);
    out << img << flag;
    

    receiver:

    bool flag;
    QByteArray ba;
    QDataStream in(ba);
    QImage temp;
    in >> temp >> flag;
        if(flag)
        {
            // got correct data
        }