Results 1 to 8 of 8

Thread: On QHTTP::request() method.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default On QHTTP::request() method.

    Hi,

    I've just received an old project which some tasks needs to be fullfilled. I know the QHttp class is deprecated and shouldn't be used but I have a time issue that forces me to perform some quicky workarounds on the code without gettings hands dirty in refactoring in order to migrate to QNetworkAccessManager.

    I have a question about QHttp class as you might guess. I wonder if do I need to use a seperate thread for network operations performed over QHttp class? As far as I remember, QNetworkAccessManager has it's own internal/seemless worker threads that gets the job done. Is it also similar for example QHttp::request() method as well? I mean do I need to create a worker thread explicitly for a network operation if I use QHttp class?

    Regards

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: On QHTTP::request() method.

    No. No user threads are required for Qt networking (until recently there were no threads used by Qt networking internally either).

  3. #3
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: On QHTTP::request() method.

    First of all, thanks for your response.

    until recently there were no threads used by Qt networking internally either
    How the long time running network operations performed then?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: On QHTTP::request() method.

    Asynchronously with signals and slots, exactly as described in the documentation.

  5. #5
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: On QHTTP::request() method.

    Pardon me. I understand what you want to point out but it's also stated in the documentation that Http::request() is not a blocking method. Thus, if I call the request from UI thread, it should process the request(QHttp::request()) in a worker thread in order not to block.

    The signal-slot mechanism you mentioned should be used to update UI thread from the worker one. Am I wrong?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: On QHTTP::request() method.

    Yes, you are wrong. You said it yourself, the request(), get(), post() and head() methods do not block the UI thread. When a request completes, the state changes, or data is available you will see a signal from the QHttp object announcing that.

    Here is a complete example showing the UI is responsive during a longer (22MB) download. Try resizing as it runs.:
    Qt Code:
    1. #include <QApplication>
    2. #include <QVBoxLayout>
    3. #include <QLabel>
    4. #include <QTimer>
    5. #include <QHttp>
    6.  
    7. class Widget: public QWidget {
    8. Q_OBJECT
    9.  
    10. QTimer *timer;
    11. QHttp *http;
    12. QLabel *timerLabel;
    13. QLabel *infoLabel;
    14. int tick;
    15. int bytes;
    16.  
    17. public:
    18. Widget(QWidget *p = 0): QWidget(p) {
    19. timerLabel = new QLabel("", this);
    20. infoLabel = new QLabel("", this);
    21. QVBoxLayout *layout = new QVBoxLayout(this);
    22. layout->addWidget(timerLabel);
    23. layout->addWidget(infoLabel);
    24.  
    25. QTimer *timer = new QTimer(this);
    26. connect(timer, SIGNAL(timeout()), SLOT(handleTimer()));
    27. timer->setInterval(1000);
    28. timer->start();
    29.  
    30. http = new QHttp(this);
    31. connect(http, SIGNAL(requestStarted(int)), SLOT(handleStarted(int)));
    32. connect(http, SIGNAL(readyRead(QHttpResponseHeader)), SLOT(handleRead(QHttpResponseHeader)));
    33. connect(http, SIGNAL(requestFinished(int,bool)), SLOT(handleFinished(int,bool)));
    34.  
    35. http->setHost("www.nasa.gov");
    36. http->get("/sites/default/files/pia17582.jpg");
    37.  
    38. tick = 0;
    39. bytes = 0;
    40. }
    41.  
    42. private slots:
    43. void handleTimer() {
    44. timerLabel->setText(QString::number(tick++));
    45. }
    46. void handleStarted(int id) {
    47. infoLabel->setText(QString("Request %1 started").arg(id));
    48. }
    49. void handleRead(const QHttpResponseHeader &header) {
    50. QByteArray ba = http->readAll();
    51. bytes += ba.size();
    52. infoLabel->setText(QString("Read %1 bytes").arg(bytes));
    53. }
    54. void handleFinished(int id, bool error) {
    55. infoLabel->setText(QString("Request %1 finished").arg(id));
    56. }
    57. };
    58.  
    59. int main(int argc, char **argv)
    60. {
    61. QApplication app(argc, argv);
    62. Widget w;
    63. w.show();
    64. return app.exec();
    65. }
    66. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to ChrisW67 for this useful post:

    zgulser (20th November 2013)

  8. #7
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: On QHTTP::request() method.

    Hi,

    I exactly understood what you told about async signal-slot messaging. I'm only saying this messaging should be a type of queued connection since signal receiver is in the UI thread where sender is not.

    Thanks for your valuable responses by the way.

  9. #8
    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: On QHTTP::request() method.

    The sender (QHttp) is also in the UI thread.
    Even if it were not, Qt::AutoConnection would take care of the thread bridiging.
    But, again, this is not the case here since there is only one thread involved.

    Cheers,
    _

Similar Threads

  1. QHttp request
    By nimsnx in forum Qt Programming
    Replies: 1
    Last Post: 2nd July 2009, 20:54
  2. Replies: 11
    Last Post: 20th January 2009, 14:10
  3. QHTTP get request
    By s410i in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2008, 17:35
  4. Difference between QHttp::get and QHttp.request
    By LMZ in forum Qt Programming
    Replies: 2
    Last Post: 6th September 2007, 16:15
  5. Qhttp::request(...) method and GET request
    By mikhailt in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2006, 12:26

Tags for this Thread

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.