V
V
Victor Nasonov2016-05-27 19:52:22
Qt
Victor Nasonov, 2016-05-27 19:52:22

What is wrong with QT QPixmap?

I'm trying to download a picture from a contact and display it.
The picture is not displayed.
main.cpp code

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h code
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QLabel *new_label= new QLabel();
    ui->mainToolBar->addWidget(new_label);
    QNetworkAccessManager *manager = new QNetworkAccessManager(0);
    QNetworkReply *netReply = manager->get(QNetworkRequest(QUrl("http://cs629111.vk.me/v629111385/3a54c/10FRUstprFo.jpg")));
    QEventLoop loop;
    connect(netReply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();
    QPixmap pixmap;
    QByteArray array = netReply->readAll();
    if (pixmap.loadFromData(array))
    {
        new_label->setPixmap(pixmap.scaled(600, 300, Qt::KeepAspectRatio));
    } else
    {
        qDebug() << "not loaded" << endl;
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.ui code
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget"/>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

I tried manually saving the image to a file, loading it via pixmap.load() and setting it via new_label->setPixmap(pixmap). The picture is showing fine.
working mainwindow.cpp code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QLabel *new_label= new QLabel();
    ui->mainToolBar->addWidget(new_label);
    QPixmap pixmap;
    pixmap.load("10FRUstprFo.jpg");
    new_label->setPixmap(pixmap);
}

MainWindow::~MainWindow()
{
    delete ui;
}

Tell me what's the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Nasonov, 2016-05-28
@kvonosan

Changed the code to
Everything worked. Question removed.

T
tugo, 2016-05-30
@tugo

Use the new signal/slot syntax https://wiki.qt.io/New_Signal_Slot_Syntax/en

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question