Results 1 to 5 of 5

Thread: SIGNAL/SLOT Calling the same function. Possible?

  1. #1
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default SIGNAL/SLOT Calling the same function. Possible?

    Hello there!
    I have a question!

    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:

    Qt Code:
    1. DownloadFirst(QList){
    2. ListOfLinks.append(QList);
    3. download(ListOfLinks.first()); // download(QUrl) emits finished()
    4. }
    5.  
    6. (SLOT) DownloadNext(){
    7. ListOfLinks.removeFirst();
    8. if(!ListOfLinks.empty()){
    9. download(ListOfLinks.first());
    10. }}
    To copy to clipboard, switch view to plain text mode 

    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!!

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: SIGNAL/SLOT Calling the same function. Possible?

    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);
    }

  3. The following user says thank you to mvuori for this useful post:

    matthieunc (28th July 2011)

  4. #3
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: SIGNAL/SLOT Calling the same function. Possible?

    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

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: SIGNAL/SLOT Calling the same function. Possible?

    Just a simple thought, why not using just one method for download (with simple pause mechanism) ?
    Qt Code:
    1. void downloadNext(){
    2. if( !downloadQueue.empty() and this->_processUrls ){
    3. const QUrl url = downloadQueue.takeFirst();
    4. downloadFile(url); // will emit finished signal connected to this method
    5. // if you dont want to process urls anymore now, set this->_processUrls = false
    6. // so it will pause after finishing current active download
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    At the beginning just push back an Url to queue and start processing by calling downloadNext()

  6. The following user says thank you to stampede for this useful post:

    matthieunc (29th July 2011)

  7. #5
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: SIGNAL/SLOT Calling the same function. Possible?

    That's so cool, thank you so much for your help!

Similar Threads

  1. Replies: 4
    Last Post: 20th January 2011, 02:03
  2. Differences in calling a Slot
    By Denarius in forum Qt Programming
    Replies: 7
    Last Post: 28th April 2009, 16:32
  3. Replies: 2
    Last Post: 14th February 2009, 21:08
  4. Replies: 12
    Last Post: 14th June 2006, 10:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.