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