Converting QImage to QByteArray using QDataStream

China☆狼群 提交于 2021-02-11 14:00:20

问题


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();

回答1:


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
    }


来源:https://stackoverflow.com/questions/61538601/converting-qimage-to-qbytearray-using-qdatastream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!