Results 1 to 3 of 3

Thread: multithread with Qhttp

  1. #1
    Join Date
    May 2012
    Posts
    33
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default multithread with Qhttp

    hi all. im trying to download web pages with QHttp with more than one thread.... here is the my... when the code arrive
    str-> http.readall();

    the application Hang... how knows whats the problem


    mainwindows.cpp


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include<QtNetwork/QHttp>
    4. #include<QDebug>
    5. #include<QMutex>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. path[10];
    13. host = "www.google.com";
    14.  
    15. for(int i=0;i<10;i++)
    16. path[i] = "/search?q=qt&start="+QString::number(i);
    17. }
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void MainWindow::on_pushButton_clicked()
    25. {
    26. qDebug()<<"A";
    27.  
    28. for(int i=0;i<10;i++)
    29. {
    30. qDebug()<<"B";
    31.  
    32. QHttp *myHttp = new QHttp("www.google.com");
    33. reqHeader = new QHttpRequestHeader("GET",path[i],1,1);
    34. qDebug()<<reqHeader->toString();
    35. connect(myHttp,SIGNAL(requestFinished(int,bool)),this,SLOT(dlwebpage(int,bool)));
    36. connect(myHttp,SIGNAL(responseHeaderReceived(QHttpResponseHeader)),this,SLOT(dlResPonseHeader(QHttpResponseHeader)));
    37. int a = myHttp->request(*reqHeader);
    38.  
    39. qDebug()<<QString::number(a);
    40. qDebug()<<"C";
    41. }
    42. qDebug()<<"Z";
    43. }
    44.  
    45. void MainWindow::dlwebpage(int id,bool error)
    46. {
    47. QMutex mutex;
    48.  
    49. mutex.lock();
    50.  
    51. qDebug()<<QString::number(id)<<" ---- " << error;
    52.  
    53. QHttp *http;
    54. http = map[id];
    55.  
    56.  
    57. qDebug()<<"D";
    58. QString str;
    59. qDebug()<<"F";
    60. str = http->readAll();
    61. qDebug()<<"E";
    62.  
    63. mutex.unlock();
    64. }
    65.  
    66.  
    67. void MainWindow::dlResPonseHeader(QHttpResponseHeader myRespHeader)
    68. {
    69. QMutex M;
    70. M.lock();
    71. qDebug()<<myRespHeader.toString();
    72. M.unlock();
    73. }
    To copy to clipboard, switch view to plain text mode 


    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include<QHttp>
    6. #include<QHttpResponseHeader>
    7. #include<QMap>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. QString path[10];
    20. QString host;
    21. QHttp *myHttp;
    22. QHttpRequestHeader *reqHeader;
    23. QMap<int,QHttp*> map;
    24.  
    25.  
    26.  
    27. ~MainWindow();
    28.  
    29. private slots:
    30. void on_pushButton_clicked();
    31. void dlwebpage(int id, bool error);
    32. void dlResPonseHeader(QHttpResponseHeader myRespHeader);
    33.  
    34.  
    35. private:
    36. Ui::MainWindow *ui;
    37. };
    38.  
    39. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th July 2012 at 08:13. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: multithread with Qhttp

    I don't see any threads in your code and your use of mutexes makes no sense. If you declare a local variable, it is not shared with a different call to the same function.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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: multithread with Qhttp

    There are no threads in your code, which is just as well because you are misusing QMutex.

    Ask yourself, where am I populating map?
    Then ask yourself, "Why am I using the deprecated QHttp class with documentation that specifically tells me not to use it in new code?"

Similar Threads

  1. Multithread in multicore CPU
    By ^NyAw^ in forum General Discussion
    Replies: 12
    Last Post: 26th December 2017, 14:19
  2. I don't quite understand this multithread example
    By HelloDan in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2009, 08:58
  3. Problem with MultiThread..
    By blm in forum Qt Programming
    Replies: 6
    Last Post: 15th September 2008, 10:48
  4. about multithread
    By Pang in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2007, 18:06
  5. Help me about multithread!
    By vql in forum Newbie
    Replies: 19
    Last Post: 8th February 2007, 15:01

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.