Important: Please read the Qt Code of Conduct -

Program stops working while loading QByteArray into QPixmap

  • I'm trying to download an image from the web and save it using a Qt Console App.
    Here is the code:

    QT += core
    QT += gui
    QT += network
    CONFIG += c++11
    TARGET = temps
    CONFIG += console
    CONFIG -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp
    HEADERS += \


    #ifndef HEAD_H
    #define HEAD_H
    #include <QCoreApplication>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QtNetwork/QNetworkRequest>
    #include <QtNetwork/QNetworkReply>
    #include <QUrl>
    #include <QEventLoop>
    #include <QString>
    #include <QPixmap>
    class desc: public QObject{
        QNetworkAccessManager nam;
        QNetworkReply* reply;
        QPixmap pm;
        desc(QString url){
            QEventLoop loop;
            reply = nam.get(QNetworkRequest(url));
    #endif // HEAD_H


    #include "head.h"
    int main(int argc, char *argv[])
        QCoreApplication a(argc, argv);
        desc img("");"test.png");
        return a.exec();

    When compiling, I get follwing error: "temps.exe has stopped working", at line 26 of head.h .
    This is the line : pm.loadFromData(reply->readAll()); .
    reply->readAll() alone doesn't cause problems, and pm.loadFromData(QByteArray()) also works.

    I've pasted the desc class in a Qt Widget app and the code worked with no problems, so I guess the problem is in the .pro file or nearby.

  • Moderators


    I don't think you can mix QPixmap with QCoreApplication. Pixmaps are dependent on the window manager running (and you don't interface any). Try with QImage instead.

  • @kshegunov

    I just replaced QPixmap pm; with QImage pm; and it worked. Thank you very much :D

Log in to reply