Results 1 to 3 of 3

Thread: multithread with Qhttp

Threaded View

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

    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

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.