I have a problem on a project im working on. What I want to do is fetching a image from a folder on a web server, and store them as a QPixmap object. Everything goes fine, and the image can be set inside the function which loads the data from the bytearray. But the QPixmap object as defined in the header file, won't hold the data for later use.
Header file:
#ifndef HTTPBILDE_H
#define HTTPBILDE_H
#include <QBuffer>
#include <QHttp>
#include <QUrl>
#include <QPixmap>
#include <QPushButton>
Q_OBJECT
public:
~HttpBilde();
public slots:
void finished(int requestId, bool error);
signals:
private:
int Request;
};
#ifndef HTTPBILDE_H
#define HTTPBILDE_H
#include <QBuffer>
#include <QHttp>
#include <QUrl>
#include <QPixmap>
#include <QPushButton>
class HttpBilde : public QObject {
Q_OBJECT
public:
HttpBilde(QString adresse);
~HttpBilde();
public slots:
void finished(int requestId, bool error);
signals:
void getPixmap(QPixmap);
private:
QBuffer *buffer;
QByteArray bytes;
QHttp *http;
int Request;
QUrl url;
QPixmap img;
};
To copy to clipboard, switch view to plain text mode
Cpp file:
}// constructor
...
url.setUrl(adresse);
http->setHost(url.host());
Request=http->get(url.path(),buffer);
connect(http, SIGNAL(requestFinished(int, bool)),this, SLOT(finished(int, bool)));
}
}
void HttpBilde::finished(int requestId, bool error) {
if (Request==requestId){
img.loadFromData(bytes);
// here I can do whatever i like with the QPixmap (img) and i get correct image data
}
}
return img;
// The returned QPixmap gets size -1,-1 no pixmap data
}
}// constructor
...
url.setUrl(adresse);
http = new QHttp(this);
buffer = new QBuffer(&bytes);
buffer->open(QIODevice::WriteOnly);
http->setHost(url.host());
Request=http->get(url.path(),buffer);
connect(http, SIGNAL(requestFinished(int, bool)),this, SLOT(finished(int, bool)));
}
}
void HttpBilde::finished(int requestId, bool error) {
if (Request==requestId){
img.loadFromData(bytes);
// here I can do whatever i like with the QPixmap (img) and i get correct image data
}
}
QPixmap HttpBilde::getPixmap() {
return img;
// The returned QPixmap gets size -1,-1 no pixmap data
}
To copy to clipboard, switch view to plain text mode
If i send a signal from void HttpBilde::finished(int requestId, bool error) to a slot in another class, the correct pixmap data is sent. And I can set the pixmap to for example a QLabel. But i encounter the same problem in the other class if I try to store it in a QPixmap object in that class.
I have also tried deep copy with no luck. It holds the data as long as it gets emited trough signals but disappears afterwards.
Bookmarks