PDA

View Full Version : SIGNAL/SLOT Calling the same function. Possible?



matthieunc
28th July 2011, 14:57
Hello there!
I have a question! :D

I'm trying to build an algorithm to download a QList of Url, each of them containing a file. Since there are some limits in downloading files in a parallel way, I'm trying to download only one file at a time.

One part of this algorithm is:



DownloadFirst(QList){
ListOfLinks.append(QList);
download(ListOfLinks.first()); // download(QUrl) emits finished()
}

(SLOT) DownloadNext(){
ListOfLinks.removeFirst();
if(!ListOfLinks.empty()){
download(ListOfLinks.first());
}}


I would connect the finished() signal to the slot (downloadNext()), and so, the downloadNext() function would be called as many times as it needs to download the whole bunch of files. My question is: Is it possible to call a function from a signal sent from inside the same funtion?

thanks for ur help!! :D

mvuori
28th July 2011, 15:22
I don't see why not.

But in that case you just make things more complicated that way, I think. Why not just loop through the list:
foreach (QUrl url, ListOfLinks) {
donwload(url);
}

matthieunc
28th July 2011, 15:42
Woooww!! I didnt know this function!!! But how can I tell the function to wait for the end of the download? The advantage of my example is that a new download starts ONLY after the signal finished() is emmited.
In your example, the download(url) will return immediatly (it's basically a QNetworkAccessManager.get(QNetworkRequest)), and this implies that the loop will keep looping without caring the signal finished()... Or is there any way to wait to tell the loop to wait for the signal? please help, I'm thirsty of knowledge :D

stampede
28th July 2011, 15:56
Just a simple thought, why not using just one method for download (with simple pause mechanism) ?


void downloadNext(){
if( !downloadQueue.empty() and this->_processUrls ){
const QUrl url = downloadQueue.takeFirst();
downloadFile(url); // will emit finished signal connected to this method
// if you dont want to process urls anymore now, set this->_processUrls = false
// so it will pause after finishing current active download
}
}

At the beginning just push back an Url to queue and start processing by calling downloadNext()

matthieunc
28th July 2011, 23:24
That's so cool, thank you so much for your help! :)