PDA

View Full Version : multithread with Qhttp



k.qasempour
11th July 2012, 08:44
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



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtNetwork/QHttp>
#include<QDebug>
#include<QMutex>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
path[10];
host = "www.google.com";

for(int i=0;i<10;i++)
path[i] = "/search?q=qt&start="+QString::number(i);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
qDebug()<<"A";

for(int i=0;i<10;i++)
{
qDebug()<<"B";

QHttp *myHttp = new QHttp("www.google.com");
reqHeader = new QHttpRequestHeader("GET",path[i],1,1);
qDebug()<<reqHeader->toString();
connect(myHttp,SIGNAL(requestFinished(int,bool)),t his,SLOT(dlwebpage(int,bool)));
connect(myHttp,SIGNAL(responseHeaderReceived(QHttp ResponseHeader)),this,SLOT(dlResPonseHeader(QHttpR esponseHeader)));
int a = myHttp->request(*reqHeader);

qDebug()<<QString::number(a);
qDebug()<<"C";
}
qDebug()<<"Z";
}

void MainWindow::dlwebpage(int id,bool error)
{
QMutex mutex;

mutex.lock();

qDebug()<<QString::number(id)<<" ---- " << error;

QHttp *http;
http = map[id];


qDebug()<<"D";
QString str;
qDebug()<<"F";
str = http->readAll();
qDebug()<<"E";

mutex.unlock();
}


void MainWindow::dlResPonseHeader(QHttpResponseHeader myRespHeader)
{
QMutex M;
M.lock();
qDebug()<<myRespHeader.toString();
M.unlock();
}



mainwindow.h




#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QHttp>
#include<QHttpResponseHeader>
#include<QMap>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
QString path[10];
QString host;
QHttp *myHttp;
QHttpRequestHeader *reqHeader;
QMap<int,QHttp*> map;



~MainWindow();

private slots:
void on_pushButton_clicked();
void dlwebpage(int id, bool error);
void dlResPonseHeader(QHttpResponseHeader myRespHeader);


private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

wysota
11th July 2012, 09:15
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.

ChrisW67
11th July 2012, 09:17
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?"