Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Program stops working while loading QByteArray into QPixmap
Forum Updated to NodeBB v4.3 + New Features

Program stops working while loading QByteArray into QPixmap

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 723 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cpperC Offline
    cpperC Offline
    cpper
    wrote on last edited by
    #1

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

    temps.pro:

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

    head.h:

    #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{
        Q_OBJECT
    public:
    
        QNetworkAccessManager nam;
        QNetworkReply* reply;
        QPixmap pm;
    
        desc(QString url){
            QEventLoop loop;
            reply = nam.get(QNetworkRequest(url));
            QObject::connect(&nam,&QNetworkAccessManager::finished,&loop,&QEventLoop::quit);
            loop.exec();
            pm.loadFromData(reply->readAll());
        }
    
    };
    
    #endif // HEAD_H
    
    

    main.cpp:

    #include "head.h"
    
    int main(int argc, char *argv[])
    {
    
        QCoreApplication a(argc, argv);
        desc img("http://www.meteoromania.ro/images/clima/temperatura_orara.png");
        img.pm.save("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.

    kshegunovK 1 Reply Last reply
    0
    • cpperC cpper

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

      temps.pro:

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

      head.h:

      #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{
          Q_OBJECT
      public:
      
          QNetworkAccessManager nam;
          QNetworkReply* reply;
          QPixmap pm;
      
          desc(QString url){
              QEventLoop loop;
              reply = nam.get(QNetworkRequest(url));
              QObject::connect(&nam,&QNetworkAccessManager::finished,&loop,&QEventLoop::quit);
              loop.exec();
              pm.loadFromData(reply->readAll());
          }
      
      };
      
      #endif // HEAD_H
      
      

      main.cpp:

      #include "head.h"
      
      int main(int argc, char *argv[])
      {
      
          QCoreApplication a(argc, argv);
          desc img("http://www.meteoromania.ro/images/clima/temperatura_orara.png");
          img.pm.save("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.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @cpper

      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.

      Read and abide by the Qt Code of Conduct

      cpperC 1 Reply Last reply
      3
      • kshegunovK kshegunov

        @cpper

        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.

        cpperC Offline
        cpperC Offline
        cpper
        wrote on last edited by
        #3

        @kshegunov

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved