[Qt-interest] Creating QPixmap from QByteArray causes segfault [SOLVED]

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Tue May 4 06:55:56 CEST 2010


This error was coming because I was trying to use a QPixmap in a
console (non-GUI) application.

It works fine if I use it in a GUI app.

I'm actually writing a helper class, which was supposed to download an
image along with some other metatdata and emit this result.

For testing this class I thought I'd make a simple console app and see
if it works. I did not want to go the GUI way since there really isn't
any GUI part to test.

Is there a way I can instantiate QPixmap's in a non-GUI app?

Thanks,
-mandeep

On Mon, May 3, 2010 at 1:52 PM, Mandeep Sandhu
<mandeepsandhu.chd at gmail.com> wrote:
> 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