Results 1 to 11 of 11

Thread: NEED HELP: How to download file without breaking function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: NEED HELP: How to download file without breaking function

    1.) Create a slot for the updateButton clicked() signal
    2.) In that slot make a QHttp object called getVersionInfo and use get() to retrieve the version file. Exit the slot and enter the event loop.
    3.) Connect the done() signal from getVersionInfo to a slot which will read the file, and determine if downloading the latest version is necessary or not
    4.) If so, create a second QHttp object called getNewUpdate and again use get() to retrieve the installer. Again exit the slot and enter the event loop.
    5.) Connect the done() signal from getNewUpdate object to another slot which will execute the installer, terminate the program, etc
    No.

    1.) Create a slot for the updateButton clicked() signal
    2.) In that slot make a QHttp object called getVersionInfo, connect requestFinished(int, bool) signal to some slot you define.
    3.) Set the host and user / pass if needed.
    4.) Use get() passing in file uri to download. I also recommend passing in an opened QFile object as the second argument, then the request will automagically save the file data for you. (I don't understand why you're talking about event loops at this point). Assign the return integer value to a member variable which you can compare in your requestFinished(int, bool) receiver slot, say m_fileGetId.
    The way the QHttp works is you can feed in loads of requests and they are processed in a queue which QHttp manages internally. At some point in future the requestFinished signal will emit passing the id return for the request as the first parameter, so you need to do something like:
    Qt Code:
    1. void Downloader::fileDownloadRequestFinished(int id, bool error)
    2. {
    3. if(id == m_fileGetId)
    4. {
    5. m_file.close();
    6.  
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Bear in mind that that m_http->setHost(...) call actually returns and id and so do the user / pass setting functions so they count as requests which will emit the signal which is why you need to keep track of the id of the FILE request.

    "How do I download a file without having to exit the current function? Do I need to make a CPU-intensive while() loop which will continiously check if the download is done, or is there a different way which I have not found out yet?"
    Let it return, what's the problem, the download will be happening in the background and when the above slot is called and the id matches the get id, then you know that the download is complete, so open it. Here's an extended version of the above example:
    Qt Code:
    1. void Downloader::fileDownloadRequestFinished(int id, bool error)
    2. {
    3. if(id == m_fileGetId)
    4. {
    5. m_file.flush();
    6. m_file.close(); // Flushes and closes the file ready for opening.
    7.  
    8. if(m_file.open(QIODevice::ReadOnly))
    9. {
    10. // load the content
    11.  
    12. m_file.close();
    13. }
    14.  
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    You need to realise that signal / slot connections make your job much simpler, dont' be afraid of them.

    The unfortunate thing about QHttp is that occasionally you may want to do job synchronously, but it's not supported by QHttp, but hey, it's a workable situation.
    If you want to hold up your ui so nothing can be done until the file is downloaded, you can put a progressbar on your ui or modal dialog and do this:
    Qt Code:
    1. connect(m_http, SIGNAL(dataReadProgress(int, int)), this, SLOT(SetProgress(int, int)));
    2.  
    3. void Downloader::SetProgress(int bytesRead, int totalBytes)
    4. {
    5. ui.progressBar->setMaximum( totalBytes );
    6. ui.progressBar->setValue( bytesRead );
    7. }
    To copy to clipboard, switch view to plain text mode 

    If you do the progressbar in a modal dialog, then guess what, you need another slot. You could define a slot on the modal progress ui which subscribes to the progressbars valueChanged ( int value ) signal, which then analyses the value of the progressbar. If it's at 100% (current value == max) then calls this->close().
    There are other ways of approaching it but it's up to you.

    BUT, if you insist on a tight while loop, then put some kind of thread "sleep for 100 ms" call in so you don't destroy the PC's performance.

  2. #2
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: NEED HELP: How to download file without breaking function

    Something else you could also do is start a new thread and get it to do the get request, but setup in a QMutex and QWaitCondition in advance say m_getMutex and m_waitCond, then just after starting the thread, call m_waitCond( &m_getMutex). In your get request thread once the requestFinished slot is called and you've closed you QFile object, call m_waitCond.wakeAll() then you main thread will only continue once the download is done.

Similar Threads

  1. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  2. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  3. Replies: 3
    Last Post: 18th October 2007, 18:07
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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
  •  
Qt is a trademark of The Qt Company.