You are not logged in.

1

Tuesday, September 7th 2004, 5:50pm

QPixmap - load Image from the Internet

Hi,

i wanna display a image on my widget. But the file is not on my local system. It is a picture on a webserver. How can i load it into a QPixmap? I know that, the QPixmap has a method loadFromData. Do I have to download the Image, using sockets and then loading it in the QPixmap? Or is it possible to do something like this:

pixmap->loadFromData(url);

I wanna download some pictures, so planned to load the image in a Pixmap and use the save method, to save it to a local file.

Mike

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

2

Tuesday, September 7th 2004, 6:03pm

You can skip the QPixmap step and just download the image using QHttp::get using the target file as the second parameter

Cheers,
_
Qt/KDE Developer
Debian User

3

Tuesday, September 7th 2004, 6:07pm

Ahhhhh oki, thank you. And if I wanna display it? I'm currently trying with this:


QByteArray respond;
QBuffer *buffer = new QBuffer(respond);
QHttp dataHttp;
dataHttp.setHost("www.myhost.de");
dataHttp.get("/myImage.gif", buffer);
myPixmap->loadFromData(respond);

But this does not seem to work. :)

Mike

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

4

Tuesday, September 7th 2004, 6:14pm

As you also want to have it as a file I suggest you download it to the target file and the load the file like any other image file (QPixmap::load())

And remember that QHttp is (like all standard Qt IO classes with signals) event loop based, so you have to return to the event loop after issuing get()

Cheers,
_
Qt/KDE Developer
Debian User

5

Tuesday, September 7th 2004, 7:42pm

Ok thank you. I will try this.

Mike

6

Tuesday, September 7th 2004, 8:17pm

Well... this does not work as supposed.

I've tried it this way:

The Pixmap tries to load the Image. If this fails, the image should be downloaded. So first I do this:

void myWidget::downloadIcon(QString icon, QString saveTo) {

QUrl IconUrl(icon);


QFile *file = new QFile(saveTo);
if (!file->open( IO_WriteOnly ) ) {
cout << "NÖÖÖÖÖÖÖ" << endl;

delete file;

return;
}

QHttp* dataHttp = new QHttp(this, "dataHttp");

dataHttp->setHost(IconUrl.host());
dataHttp->get(IconUrl.dirPath() + "/" + IconUrl.fileName(), file);

IconFileDict.insert(dataHttp->currentId(), file);
IconHttpDict.insert(dataHttp->currentId(), dataHttp);

connect(dataHttp, SIGNAL(requestFinished (int, bool)), this, SLOT(downloadIconFinished(int, bool)));

}

IconFileDict and IconHttpDict are QIntDicts. So I have the possibility to access the File and the Http pointer by using the Id of Http, which is a parameter of requestFinished.

The problem is: The download works. But: After downloading I can not load the image. I have to restart the program. I think this is, because the file is still opened. So my idea was to close the file in the Slot downloadIconFinished. I have the id, so I have the pointer to the file, by using IconFileDict. But:

void myWidget::downloadIconFinished(int id, bool error) {

cout << "Id: " << id << endl;
IconHttpDict[id]->closeConnection();
delete IconHttpDict[id];
IconHttpDict.remove(id);

IconFileDict[id]->close();
delete IconFileDict[id];
IconFileDict.remove(id);

if (!error) {
reloadImage();
}


}

closeConnection or closing the file let the program crash. Why? I wanted to close the file, so the downloaded file can be used by the program. I tried the slot done(bool), instead of RequestFinished. But here I don't have a Id, so I don't have access to the file-pointer.

Do you have any ideas?

Mike

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

7

Wednesday, September 8th 2004, 8:40pm

Quoted

Originally posted by miketech
Do you have any ideas?


You are using the wrong id, use the one returned by get()
Currently you are using the one of setHost() as this is the first operation in the queue and currentId() returns that one.

Cheers,
_
Qt/KDE Developer
Debian User