Results 1 to 6 of 6

Thread: Problem with curl writefunction

  1. #1
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Problem with curl writefunction

    Hello.
    I have little problem.
    I have functions:
    Qt Code:
    1. size_t WriteData(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    2. size_t written;
    3. written = fwrite(ptr, size, nmemb, stream);
    4. return written;
    5. }
    6. int MainWindow::progress_func(void* ptr, double TotalToDownload, double NowDownloaded, double TotalToUpload, double NowUploaded)
    7. {
    8. ui->progressBar->setMaximum(TotalToDownload);
    9. ui->progressBar->setValue(NowDownloaded);
    10. double speed;
    11. curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &speed);
    12. ui->label_2->setText(QString("Prędkość: %0").arg(speed));
    13. return NowDownloaded;
    14. }
    15. bool MainWindow::downloadFile(QString url, QString filename)
    16. {
    17. FILE *fp;
    18. fp = fopen(filename.toStdString().c_str(), "wb");
    19. ui->label->setText(filename);
    20. ui->label_2->setText("Predkosc: 0 KB/s");
    21. curl = curl_easy_init();
    22. curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1);
    23. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
    24. curl_easy_setopt(curl, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (compatible;)");
    25. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie");
    26. curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie");
    27. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
    28. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);
    29. curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    30. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
    31. curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
    32. curl_easy_setopt(curl, CURLOPT_URL, url.toStdString().c_str());
    33. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
    34. curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &MainWindow::progress_func);
    35. curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
    36. status = curl_easy_perform(curl);
    37. if(status !=0)
    38. qDebug() << QString("curl_easy_perform() error: %0").arg(curl_easy_strerror(status));
    39. curl_easy_cleanup(curl);
    40. fclose(fp);
    41. return true;
    42. }
    To copy to clipboard, switch view to plain text mode 
    When I try to download a file, window of my aplication is frozen(?) until downloading is not finished.
    Could you suggest what could I do to make this work?
    Sory for my bad english
    Janusz
    Last edited by januszmk; 26th July 2011 at 15:48.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with curl writefunction

    A solution could be to execute this function in a separate thread.

    Why you use CURL and not QtNetwork?
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with curl writefunction

    Thanks, when I execute function in another thread, it's start working. But progress function is still not working:
    Qt Code:
    1. int MainWindow::progress_func(void* ptr, double TotalToDownload, double NowDownloaded, double TotalToUpload, double NowUploaded)
    2. {
    3.  
    4. int total = (int)TotalToDownload;
    5.  
    6. ui->progressBar->setMaximum(total);
    7. int now = (int)NowDownloaded;
    8. ui->progressBar->setValue(now);
    9. qDebug() << QString("%0 : %1").arg(now).arg(total);
    10. double speed;
    11. curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &speed);
    12. int speed1 = (int)speed;
    13. ui->label_2->setText(QString("Predkosc: %0 B/s").arg(speed1));
    14. // qDebug() << speed;
    15. return NowDownloaded;
    16. }
    To copy to clipboard, switch view to plain text mode 
    now and total always = 0 why?
    I am using curl because I have more experience with curl (from php), and in QtNetwork even if I used cookies, websites detect then I have disabled cookies.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with curl writefunction

    First of all, these line are wrong

    Qt Code:
    1. qDebug() << QString("%0 : %1").arg(now).arg(total);
    2. ui->label_2->setText(QString("Predkosc: %0 B/s").arg(speed1));
    To copy to clipboard, switch view to plain text mode 

    the placeholder for arg are 1 based so you have to rewrite them as

    Qt Code:
    1. qDebug() << QString("%1 : %2").arg(now).arg(total);
    2. ui->label_2->setText(QString("Predkosc: %1 B/s").arg(speed1));
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with curl writefunction

    With arg there are no problem, because speed1 is showing property, but when I update progressbar, then i don't get property value. the value of double variable is something like that: "5.39501e-315" and when I am converting like that:
    Qt Code:
    1. int var = (int)doublevar;
    To copy to clipboard, switch view to plain text mode 
    then I get value 0

    Edit:
    I tried to do this like that:
    Qt Code:
    1. int total = 100;
    2. double temp = NowDownloaded/TotalToDownload;
    3. temp = temp*100;
    4. int total = round(temp);
    To copy to clipboard, switch view to plain text mode 
    But when I downloading file, then on the start i get 99% and when i get couple of hundred KB (total is 19MB) then I get 100%
    Last edited by januszmk; 26th July 2011 at 22:19.

  6. #6
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with curl writefunction

    I make this work by:
    Qt Code:
    1. double bytesRead;
    2. curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &bytesRead);
    3. double totaltoread;
    4. curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &totaltoread);
    To copy to clipboard, switch view to plain text mode 
    Thanks for all

Similar Threads

  1. Curl in QT Creator
    By januszmk in forum Newbie
    Replies: 10
    Last Post: 25th August 2011, 23:14
  2. cURL & QWebKit don't mix
    By Frankenstein Coder in forum Newbie
    Replies: 1
    Last Post: 20th July 2011, 03:03
  3. cURL With Qt Creator [Windows and Linux]
    By Chiggins in forum Qt Tools
    Replies: 1
    Last Post: 24th June 2010, 08:29
  4. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 20:47

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.