Results 1 to 3 of 3

Thread: Problem in using QHttp with QTimer

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

    Default Problem in using QHttp with QTimer

    Given file names and time out periods, I've to download them. If the time period expires for a file, the download is canceled.

    Initially, for debugging purpose, I display a message box informing that the download is successfully finished. This works for download of a single file in the main application. But problem arises when I try to download more than one file. The program just hangs!

    Another problem occurs when I comment out that message box display. This time, in case of a single file, the download is finished completely. But the process doesn't exit. The task manager shows that process even after download is complete. As for multiple file download, the problem, the problem is same - the program hangs.

    I have been trying to figure out the problem.

    Please help me. Here's the code:

    CTimedHttp header file:

    Qt Code:
    1. #ifndef TIMEDHTTP_H
    2. #define TIMEDHTTP_H
    3.  
    4. #include <QHttp>
    5.  
    6. class QFile;
    7. class QTimer;
    8.  
    9. class CTimedHttp : public QHttp
    10. {
    11. Q_OBJECT
    12.  
    13. private:
    14. // Default time out period.
    15. static const long INFINITE_TIME = 1000000; // 1000 seconds
    16.  
    17. // Required to initialize HTTP get ID with an improbable integer
    18. static const long INVALID_GET_ID = -10;
    19.  
    20. // Indicates whether the request times out or not.
    21. bool m_bIsTimedOut;
    22.  
    23. // Indicates whether the request finished without any interruption or not
    24. bool m_bIsRequestFinished;
    25.  
    26. // Unique ID returned by HTTP get() request
    27. long m_lHttpGetId;
    28.  
    29. // File to be downloaded
    30. QFile *m_pFile;
    31.  
    32. QTimer *timer;
    33.  
    34. private:
    35. void initialize();
    36.  
    37. private slots:
    38. void requestTimeout();
    39. void timedHttpRequestFinished(int requestId, bool error);
    40.  
    41. public:
    42. CTimedHttp(QObject* parent = 0);
    43. CTimedHttp(const QString& hostName, quint16 port = 80, QObject* parent = 0);
    44. CTimedHttp(const QString& hostName, QHttp::ConnectionMode mode, quint16 port = 80, QObject *parent = 0);
    45.  
    46. void setTimedOut(bool bIsTimedOut);
    47. bool isTimedOut();
    48.  
    49. void setRequestFinished(bool bIsRequestFinished);
    50. bool isRequestFinished();
    51.  
    52. long getHttpGetId();
    53. void setHttpGetId(long lHttpGetId);
    54.  
    55. void get(const QString& strPath, long lTimeoutPeriod = INFINITE_TIME);
    56. };
    57.  
    58. #endif
    To copy to clipboard, switch view to plain text mode 

    CTimedHttp cpp file:
    [ That message box statement is in timedHttpRequestFinished() function ]

    Qt Code:
    1. #include <QtNetwork>
    2. #include <QMessageBox>
    3.  
    4. #include "CTimedHttp.h"
    5.  
    6.  
    7. CTimedHttp::CTimedHttp(QObject* parent /* = 0 */) : QHttp(parent)
    8. {
    9. initialize();
    10. }
    11.  
    12. CTimedHttp::CTimedHttp(const QString& hostName, quint16 port /* = 80 */, QObject* parent /* = 0 */) : QHttp(hostName, port, parent)
    13. {
    14. initialize();
    15. }
    16.  
    17. CTimedHttp::CTimedHttp(const QString& hostName, ConnectionMode mode, quint16 port /* = 80 */, QObject *parent /* = 0 */)
    18. : QHttp(hostName, mode, port, parent)
    19. {
    20. initialize();
    21. }
    22.  
    23. void CTimedHttp::initialize()
    24. {
    25. setTimedOut(false);
    26. setRequestFinished(false);
    27. setHttpGetId(INVALID_GET_ID);
    28.  
    29. m_pFile = 0;
    30.  
    31. connect(this, SIGNAL(requestFinished(int, bool)), this, SLOT(timedHttpRequestFinished(int, bool)));
    32. }
    33.  
    34. void CTimedHttp::setTimedOut(bool bIsTimedOut)
    35. {
    36. m_bIsTimedOut = bIsTimedOut;
    37. }
    38.  
    39. bool CTimedHttp::isTimedOut()
    40. {
    41. return m_bIsTimedOut;
    42. }
    43.  
    44. void CTimedHttp::setRequestFinished(bool bIsRequestFinished)
    45. {
    46. m_bIsRequestFinished = bIsRequestFinished;
    47. }
    48.  
    49. bool CTimedHttp::isRequestFinished()
    50. {
    51. return m_bIsRequestFinished;
    52. }
    53.  
    54. void CTimedHttp::setHttpGetId(long lHttpGetId)
    55. {
    56. m_lHttpGetId = lHttpGetId;
    57. }
    58.  
    59. long CTimedHttp::getHttpGetId()
    60. {
    61. return m_lHttpGetId;
    62. }
    63.  
    64. void CTimedHttp::get(const QString& strPath, long lTimeoutPeriod)
    65. {
    66. QUrl url(strPath);
    67. QFileInfo fileInfo(url.path());
    68. QString fileName = fileInfo.fileName();
    69.  
    70. if (fileName.isEmpty() == true)
    71. {
    72. // [TODO] : warn the user
    73. QMessageBox::critical(0, "ERROR", "You haven't specified any file name!", QMessageBox::Ok);
    74.  
    75. return;
    76. }
    77.  
    78. m_pFile = new QFile(fileName);
    79. if (m_pFile->open(QIODevice::WriteOnly) == false)
    80. {
    81. // [TODO] : warn the user
    82. QMessageBox::critical(0, "ERROR", "Cann't open the file!", QMessageBox::Ok);
    83.  
    84. delete m_pFile;
    85. m_pFile = 0;
    86.  
    87. return;
    88. }
    89.  
    90. /* setHost(url.host()); // default to port 80
    91. m_lHttpGetId = QHttp::get(url.path(), m_pFile);
    92.  
    93. QTimer::singleShot(lTimeoutPeriod, this, SLOT(requestTimeout()));
    94.  
    95. */
    96. setHost(url.host()); // default to port 80
    97.  
    98. timer = new QTimer();
    99. connect(timer, SIGNAL(timeout()), this, SLOT(requestTimeout()));
    100. timer->setSingleShot(true);
    101.  
    102. timer->start(lTimeoutPeriod);
    103. m_lHttpGetId = QHttp::get(url.path(), m_pFile);
    104.  
    105. }
    106.  
    107. void CTimedHttp::requestTimeout()
    108. {
    109. if (isRequestFinished() == true)
    110. {
    111. return;
    112. }
    113.  
    114. setTimedOut(true);
    115. abort();
    116.  
    117. m_pFile->close();
    118. m_pFile->remove();
    119. delete m_pFile;
    120. m_pFile = 0;
    121.  
    122. // DEBUG
    123. QMessageBox::critical(0, "ERROR", "Request timed out!", QMessageBox::Ok);
    124. }
    125.  
    126. void CTimedHttp::timedHttpRequestFinished(int iRequestId, bool bError)
    127. {
    128. if (isTimedOut() == true
    129. || iRequestId != getHttpGetId()) // not the intended request
    130. {
    131. return;
    132. }
    133.  
    134. timer->stop();
    135.  
    136. setRequestFinished(true);
    137.  
    138. if (bError == true)
    139. {
    140. m_pFile->remove();
    141.  
    142. // [TODO] : What in case of error?
    143. QMessageBox::critical(0, "ERROR", "The request finished with error!", QMessageBox::Ok);
    144.  
    145. return;
    146. }
    147.  
    148. // DEBUG]
    149. // If I comment out this statement, the process does not exit
    150. QMessageBox::information(0, "Info", "Download is successful", QMessageBox::Ok);
    151.  
    152. m_pFile->close();
    153. delete m_pFile;
    154. m_pFile = 0;
    155. }
    To copy to clipboard, switch view to plain text mode 

    Main file:

    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "CTimedHttp.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. CTimedHttp* timedHttp = new CTimedHttp();
    10. timedHttp->get("http://www.google.com.bd/index.html", 60000);
    11. timedHttp->get("http://ferdous.newsit.es/routine.html", 60000);
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in using QHttp with QTimer

    Quote Originally Posted by Ferdous View Post
    This works for download of a single file in the main application. But problem arises when I try to download more than one file. The program just hangs!
    Your implementation is not suited for multiple downloads. You have just a single set of variables that control the download state, so you can't share them between multiple downloads. For example, when you invoke get() for the second time, you overwrite the m_pFile.

    QHttp::get() returns an id and both requestStarted() and requestFinished() signals also carry it. You can use it to distinguish between multiple downloads.

    Note also that QHttp will start its work after you start the event loop. That is when the program flow reaches "return app.exec()" line.


    Quote Originally Posted by Ferdous View Post
    Another problem occurs when I comment out that message box display. This time, in case of a single file, the download is finished completely. But the process doesn't exit.
    As long as the event loop is running, your application will run. By default Qt stops the event loop when you close the last window. If there are no windows, you can't close the last one, so your application keeps running. You will have to stop the event loop yourself.

  3. The following user says thank you to jacek for this useful post:

    Ferdous (6th September 2008)

  4. #3
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in using QHttp with QTimer

    Thanks for your help.

    I've added a signal
    Qt Code:
    1. void quitApplication()
    To copy to clipboard, switch view to plain text mode 
    which is connected to quit() slot
    Qt Code:
    1. connect(this, SIGNAL(quitApplication()), qApp, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 
    and is emitted at the end of requestTimeout() and timedHttpRequestFinished() function. Now download for a single file works fine.

Similar Threads

  1. QTimer problem
    By rajeshs in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2008, 14:33
  2. PyQt QTimer problem { FIXED }
    By WinchellChung in forum Newbie
    Replies: 0
    Last Post: 1st March 2008, 16:50
  3. QHttp download file problem.
    By fengtian.we in forum Qt Programming
    Replies: 12
    Last Post: 12th June 2007, 09:39
  4. QHttp Problem
    By musaulker in forum Newbie
    Replies: 4
    Last Post: 29th March 2007, 00:40
  5. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54

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.