PDA

View Full Version : How to use Qhttp for downloading?? with the following code..



Gokulnathvc
17th March 2011, 06:22
void MyClass::doSth()
> > {
> > QHttp http( "www.google.com" );
> > connect(&http, SIGNAL(done(bool)), this, SLOT(httpdone(bool)));
> > QFile file( "temp.txt" );
> > file.open( QIODevice::WriteOnly );
> > mutex.lock();
> > http.get( "/" );
> > fin.wait( &mutex );
> > file.close();
> > mutex.unlock();
> > }
> >
> > (protected slots:)
> >
> > void MyClass::httpdone(bool error)
> > {
> > if (error == false)
> > {
> > fin.wakeAll();
> > }

The file is not getting downloaded.. please help me

squidge
17th March 2011, 08:28
Don't use QHttp, the class is obsolete. Use QNetworkAccessManager. See the examples that come with Qt, and your previous topics on this very same subject.

Gokulnathvc
17th March 2011, 09:43
One problem in using the QNetworkAccessManager, that is the final loop finished loop is called only at the final execution of the program. So that the download cannot be achieved in the program only after the complete execution. The functions going to use the downloaded file fails.Please give me a solution.

squidge
17th March 2011, 10:05
So your application attempts to do network access when your program terminates? So you need to do this before you exit your main event loop. You can't expect signals and slots to work without a functioning event loop.

Gokulnathvc
17th March 2011, 11:05
Yes thats correct, How to make this done before my main event??

squidge
17th March 2011, 11:24
Before? You were saying above that it was "at the final execution of the program".

Which is it?

Gokulnathvc
17th March 2011, 11:45
Yes. readReady() and finished() functions under startRequest() using QNetworkAccessManager??.... In which readReady() and finished() functions are called only at the final execution of the program..

Gokulnathvc
18th March 2011, 08:24
Simplest way to download from the HTTP is below::


connect(&http,SIGNAL(done(bool)),this,SLOT(httpdone())); //added in the constructor..

In the header file......Make the following changes...


QHttp http;
QFile file;

private slots:
bool on_pushButton_clicked();
void httpdone();

signals:
void done();

in the Qthttp.cpp file...

bool QtHttp::on_pushButton_clicked()
{

QString strUrl="http://www.blabla.com//file";
QUrl url = QUrl::fromUserInput(strUrl);

QFileInfo fileInfo(url.path());
QString strhost=url.encodedHost();
QString filename=fileInfo.fileName();
file.setFileName("C:\\Qt\\QHttp\\"+filename);
http.setHost(url.host(),url.port(80));
http.get(url.path(),&file);

if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::warning(NULL,"warning","file is not opened",QMessageBox::Ok);
}
file.write(http.readAll());
http.close();
return true;

}

void QtHttp::httpdone()
{

file.close();
Q_EMIT done();
}

ChrisW67
18th March 2011, 08:46
What are you trying to achieve? Is it some sort of "download a set of data before the main window shows" problem?



#include <QtGui>
#include <QtNetwork>
#include <QDebug>

class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
QWidget *central = new QWidget(this);
QPushButton *button1 = new QPushButton("Button", this);
QLabel *label1 = new QLabel("Label", this);

QVBoxLayout *layout = new QVBoxLayout(central);
layout->addWidget(button1);
layout->addWidget(label1);

central->setLayout(layout);
setCentralWidget(central);
}
public slots:

private:
};

class Downloader: public QObject {
Q_OBJECT
public:
Downloader(QObject *p = 0): QObject(p) {
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));

}

void start() {
manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));
}
private slots:
void replyFinished(QNetworkReply *reply) {
// Save the received data
qDebug() << "Saving the received data";
//
emit done();
}
signals:
void done();
private:
QNetworkAccessManager *manager;
};


int main(int argc, char *argv[])
{
QApplication app(argc, argv);


MainWindow m; // create but do not show()
Downloader d;
QObject::connect(&d, SIGNAL(done()), &m, SLOT(show()));
d.start();

return app.exec();
}
#include "main.moc"