Results 1 to 5 of 5

Thread: problem in using QHttp

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default problem in using QHttp

    I've written a class that will handle file downloads. I think my logic is ok but the program isn't working as expected. The program downloads the first file successfully but hangs with the second download. I've gone through my written code over and over again but can't find the logical error.

    I've maintained one QHttp and one QFile object in the class and scheduled one GET request at a time. The idea has been taken from the book - C++ GUi programming with Qt.

    Please help me in finding out the logical error.

    myhttp.h

    Qt Code:
    1. #ifndef MYHTTP_H
    2. #define MYHTTP_H
    3.  
    4. #include <QObject>
    5.  
    6. class QHttp;
    7. class QFile;
    8.  
    9. class MyHttp : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MyHttp(QObject* parent = 0);
    15. void get(const QString& strFullFilePath);
    16.  
    17. signals:
    18. void done();
    19.  
    20. private slots:
    21. void myHttpRequestDone(bool error);
    22.  
    23. private:
    24. void processNextRequest();
    25.  
    26. QHttp* http;
    27. QFile* file;
    28. QStringList* pendingRequestList;
    29. };
    30.  
    31. #endif
    To copy to clipboard, switch view to plain text mode 


    myhttp.cpp :

    Qt Code:
    1. #include <QtNetwork>
    2.  
    3. #include "myhttp.h"
    4.  
    5. MyHttp::MyHttp(QObject* parent /* = 0 */) : QObject(parent)
    6. {
    7. http = new QHttp;
    8. file = 0;
    9. pendingRequestList = new QStringList;
    10.  
    11. connect(http, SIGNAL(done(bool)), this, SLOT(myHttpRequestDone(bool)));
    12. }
    13.  
    14. void MyHttp::get(const QString& strFullFilePath)
    15. {
    16. // Add this request to list
    17. pendingRequestList->append(strFullFilePath);
    18.  
    19. if (pendingRequestList->size() == 1)
    20. {
    21. processNextRequest();
    22. }
    23. }
    24.  
    25. void MyHttp::processNextRequest()
    26. {
    27. if (pendingRequestList->isEmpty() == true)
    28. {
    29. emit done();
    30. }
    31. else
    32. {
    33. // Get the first request in the list
    34. QString strFullFilePath = pendingRequestList->first();
    35.  
    36. // Extract the file name
    37. QUrl url(strFullFilePath);
    38. QFileInfo fileInfo(url.path());
    39. QString fileName = fileInfo.fileName();
    40.  
    41. // Create an instance of a file with the extracted name and open it
    42. file = new QFile(fileName);
    43. file->open(QIODevice::WriteOnly);
    44.  
    45. // Send the request
    46. http->setHost(url.host(), url.port(80));
    47. http->get(url.path(), file);
    48. }
    49. }
    50.  
    51. void MyHttp::myHttpRequestDone(bool error)
    52. {
    53. file->close();
    54.  
    55. if (error == true)
    56. {
    57. file->remove();
    58. }
    59.  
    60. delete file;
    61. file = 0;
    62.  
    63. pendingRequestList->removeFirst();
    64. processNextRequest();
    65. }
    To copy to clipboard, switch view to plain text mode 


    main.cpp :

    Qt Code:
    1. #include <QCoreApplication>
    2.  
    3. #include "myhttp.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication app(argc, argv);
    8.  
    9. MyHttp* myhttp = new MyHttp();
    10. QObject::connect(myhttp, SIGNAL(done()), &app, SLOT(quit()));
    11.  
    12. myhttp->get("http://thedailystar.net/images/tdsmainlogo.jpg");
    13. myhttp->get("http://thedailystar.net/photo/2008/09/06/2008-09-06__front02.jpg");
    14. myhttp->get("http://ahmadferdous.googlepages.com/Ferdous.JPG");
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by Ferdous; 9th September 2008 at 10:52. Reason: further elaboration

Similar Threads

  1. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  2. QHttp download file problem.
    By fengtian.we in forum Qt Programming
    Replies: 12
    Last Post: 12th June 2007, 09:39
  3. Problem : use QHttp get a file to QFile
    By fengtian.we in forum Qt Programming
    Replies: 9
    Last Post: 24th May 2007, 10:58
  4. QHttp Problem
    By musaulker in forum Newbie
    Replies: 4
    Last Post: 29th March 2007, 00:40
  5. QHttp "PUT METHOD" QT Problem File Upload. RFC 2616
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2006, 22:02

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.