Results 1 to 4 of 4

Thread: What the best way to trigger a download from a webpage using QWebEngine?

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default What the best way to trigger a download from a webpage using QWebEngine?

    I am using QWebEngine to mine some HTML data from a list of websites, some of this data is pdf documents that must be downloaded from the webpage. I would like to know if there is a way to programmatically trigger a download of a pdf document onto a local file system. If it is possible, is there a way to avoid pausing program execution while the downloading is in progress but instead continue to another webpage where I can initiate another download. The plan is to have 5 downloads running at a time and the rest will be queued. I am not looking for code, i just would like to know how you would approach this problem. Thanking you in advance.
    Last edited by ayanda83; 14th December 2016 at 10:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What the best way to trigger a download from a webpage using QWebEngine?

    If you can get the URL of the PDF you could just download it with a separate QNetworkAccessManager.

    If the download is not provided as a link, you'll probably need to trigger the download action via script.
    The QWebEngineProfile seems to have a signal that could be useful for managing downloads by the webegine itself, which it will highly likely do in the background automatically.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What the best way to trigger a download from a webpage using QWebEngine?

    Thank you for your reply. Below is a piece of code I wrote prior to your reply but it doesn't really work the way I want it. I can retrieve the pdf url's just fine, using QWebEnginePage::runJavaScript() but I haven't use QNetworkAccessManager before and I don't really know how to handle the download inside the callback function.
    Qt Code:
    1. javaScript_2.append("function downloadAttachment(){"
    2. "var attachmentTag = document.querySelectorAll(\"div.detailsValue > a\");"
    3. "var downloadLinks = [];"
    4. "var k;"
    5. "for(k = 0; k < attachmentTag.length; k++){"
    6. "downloadLinks[k] = attachmentTag[k].getAttribute(\"href\");"
    7. "}"
    8. "return downloadLinks;"
    9. "}"
    10. "downloadAttachment();");
    11.  
    12. runJavaScript(javaScript_2, [&](const QVariant downloadLinks){
    13. QStringList dLinks = downloadLinks.toStringList();
    14.  
    15. QNetworkAccessManager nManager;
    16. QNetworkReply *reply;
    17.  
    18. for(int i = 0; i < dLinks.size(); i++)
    19. {
    20. QNetworkRequest request(QUrl(QString("http://web1.capetown.gov.za%1").arg(dLinks.at(i))));
    21. reply = nManager.get(request);
    22. }
    23.  
    24. });
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What the best way to trigger a download from a webpage using QWebEngine?

    First you need to have the QNetworkAccessManager either as a member or on the heap, because you don't want it to be deleted when your function's scope ends.

    For the download handling, connect to the QNetworkReply's readyRead() signal and the finished() signal (or just the finished signal if the files aren't that big).

    My recommendation would be to have a Download task object that gets the QNetworkReply object and then handles everything for that single download.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    ayanda83 (15th December 2016)

Similar Threads

  1. Using QWebEngine to login to a page.
    By ayanda83 in forum Qt Programming
    Replies: 7
    Last Post: 29th November 2016, 12:17
  2. Using cache with QNetworkAccessManager in QWebEngine
    By d1psy in forum Qt Programming
    Replies: 5
    Last Post: 18th August 2016, 13:20
  3. QWebEngine MP4 5.5
    By RolandHughes in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2015, 23:04
  4. Replies: 1
    Last Post: 19th June 2014, 09:09
  5. Replies: 3
    Last Post: 17th February 2010, 12:26

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.