Network download
-
I'm trying to port an old MFC dialog to Qt. This dialog is used to download multiple files from an internet server into my app. The way it works now is it calls a PHP file on the server which then returns a simple text file that is a pipe separated list of all the files available. It then separates those files into a vector of strings and goes into a loop which downloads each file one by one. It does this in a separate thread so as not to block the UI, but otherwise it's completely synchronous so I know exactly where I am in the loop.
I looked at the download file example in the Qt docs but it's only designed to download one file and uses a slot/signal to process each file so it's asynchronous. So I'm not sure how exactly to duplicate my functionality. Do I need multiple QNetworkManager objects pointing to different slots? (i.e. one to process the list and one to process the actual file downloads) Or is there some better way to do this that I'm not seeing?
-
I'm trying to port an old MFC dialog to Qt. This dialog is used to download multiple files from an internet server into my app. The way it works now is it calls a PHP file on the server which then returns a simple text file that is a pipe separated list of all the files available. It then separates those files into a vector of strings and goes into a loop which downloads each file one by one. It does this in a separate thread so as not to block the UI, but otherwise it's completely synchronous so I know exactly where I am in the loop.
I looked at the download file example in the Qt docs but it's only designed to download one file and uses a slot/signal to process each file so it's asynchronous. So I'm not sure how exactly to duplicate my functionality. Do I need multiple QNetworkManager objects pointing to different slots? (i.e. one to process the list and one to process the actual file downloads) Or is there some better way to do this that I'm not seeing?
@Dan203 said in Network download:
Do I need multiple QNetworkManager objects pointing to different slots? (i.e. one to process the list and one to process the actual file downloads)
Nope, you use a single QNetworkAccessManager and call
get()on it multiple times. Each time you call get(), it returns a uniqueQNetworkReplyobject.The downloads occur asynchronously, as you have found. Each QNetworkReply will emit its own
finished()signal when it has finished downloading. (The QNetworkAccessManager also emits afinished()that contains the pointer to the relevant QNetworkReply)Connect the
finished()signal to a slot. In the slot, you can callQNetworkReply::url()to see what was downloaded. Based on the URL, you can either process the file contents as a list, or save the file contents to disk. -
@Dan203 said in Network download:
Do I need multiple QNetworkManager objects pointing to different slots? (i.e. one to process the list and one to process the actual file downloads)
Nope, you use a single QNetworkAccessManager and call
get()on it multiple times. Each time you call get(), it returns a uniqueQNetworkReplyobject.The downloads occur asynchronously, as you have found. Each QNetworkReply will emit its own
finished()signal when it has finished downloading. (The QNetworkAccessManager also emits afinished()that contains the pointer to the relevant QNetworkReply)Connect the
finished()signal to a slot. In the slot, you can callQNetworkReply::url()to see what was downloaded. Based on the URL, you can either process the file contents as a list, or save the file contents to disk.