[Qt-interest] Creating QPixmap from QByteArray causes segfault

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Mon May 3 10:22:10 CEST 2010


Hi All,

I'm trying to create a QPixmap from image data received from the
network. I have connected the QNAM's finshed signal to my slot, which
reads the data stream from the reply object (after all necessary error
checks) and tries creating a QPixmap object using the loadFromData()
API. However, this does not work and causes a seg fault! If I convert
the byte array to a const char* then it doesn't seg fault but gives
out the following error:

Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
JPEG datastream contains no image

The reply->bytesAvailable() shown in the finished signal exactly
correspond to the number of bytes of the image. The image is not
corrupt as I can see it eog/gqview/display progs w/o any error.

Here's the snippet that converts the data to image:

void getData(QNetworkReply* reply) {
    qDebug() << "Status code"
             <<
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

    if (reply->error() != QNetworkReply::NoError) {
        qDebug() << "Error fetching data from" << reply->url();
        return;
    }

    qDebug() << "Bytes available" << reply->bytesAvailable();
    if (reply->bytesAvailable() == 0) {
        return;
    }

    QByteArray bytes = reply->readAll();
    qDebug() << "Rx'ed" << bytes.size();

    QPixmap pixi;
    pixi.loadFromData(bytes.data());

    reply->deleteLater();
}

I'm sure ppl have done this before and got it working.

Any clues as to what could be going wrong?

Thanks,
-mandeep



More information about the Qt-interest-old mailing list