PDA

View Full Version : QHTTP problem,download multi-file.



fengtian.we
27th May 2007, 12:07
I konw the QHTTP works asynchronously

for download some files , I must do "QApplication.exec()"

so , how can I download multi-file in my Qt application??

creat thread? (QThread?) how?

run is in backgroud? how?

....

and I want when someone file is download finish ,to process it.

can you give me some idea??

marcel
27th May 2007, 12:15
No, you don't need a QThread.
To download more than one file you should first create a list of files to be copied.

You pass all these files to QHttp::get( const QString&, QIODevice* ) one by one, and you also must queue the request ID's returned by get.
By passing a valid QFile to get() QHttp will download the file to the location specified by the QFile.

You connect the QHttp::requestFinished( int id, bool ) signal to aslot in your class.
This signal will be emitted( among other situations) when a file has finished downloading, with the ID returned by get for that file.
When the id matches to one of the get() queued id's, it means that the file finished downloading, so you can process it further.

Please note that this is a rudimentary implementation.
You will also have to do other things, like error handling, both for downloading and connecting.

EDIT:
you should take a look at the HTTP client example in the Qt Demos. It downloads only a file at a time, but should give you an idea about it's general usage.

regards

fengtian.we
27th May 2007, 15:42
My code:



#include <QUrl>
NOTE: I run it step to step ( VS2005 debug function ).
#include <QtGui/QApplication>
#include <QHttp>
#include <QFile>
int main(int argc, char *argv[])
{
QApplication *app=new QApplication(argc, argv);
QFile *file=new QFile("index.php");
if(!file->open(QIODevice::WriteOnly)) //file create
{ QMessageBox::information(0,"Get User's Profile","Error: Cannot open["+file->fileName()+"] "+file->errorString());
return 0;
}
QUrl *url=new QUrl("http://localhost/a2gold/index.php");
QHttp *http=new QHttp(url->host());
http->get(url->path(),file);
http->close();
file->flush(); //Now,file is empty
return app->exec(); //since there,file is valid(have content)}


How can I get a valid(have content) file before the "return app->exec()"

I want use the file content, but I must finish the application for get a valid(have content) file .


I see the example already , but I can't understand it :crying:

marcel
27th May 2007, 17:52
I will give you a few pointers ( I have just seen that you already posted this some time ago :) ).

I assume that your program will not use GUI.

For downloading multiple files create a class derived from QObject.
You should have at least the following methods and members:
- a QList member in which you will store the queued files;
- a QListMember in which you store the get() ID's;
- addFile( const QString& ) - this will add a file to the download list;
- startDownloading() - this starts the downloading process.
- a SLOT called httpRequestFinished( int, bool ) - which will be connected to the requestFinishedSignal.
- a SIGNAL called downloadNextFile() - this will be emitted from httpRequestFinished() when a file has finished downloading.
- a SLOT called nextFile() - this will be connected to downloadNextFile - it just starts downloading the next file in the list.
- an error handling routine

By doing this you will try to follow all the basic states of QHttp.
When all the files have finished downloading or an error has occurred you must call ext() or quit() to exit the application.

Regards

fengtian.we
28th May 2007, 03:46
Thank you Marcel ~

I think I get some pointers now~ :) thank you help~

an other porblem:

in the <C++ GUI Programming with Qt 4 >:
"The QHttp class works asynchronously. When we call a function like get() or post(), the function returns immediately, and the data transfer occurs later, when control returns to Qt's event loop."


what is Qt's event loop???

marcel
28th May 2007, 05:45
QHttp uses inside a QTimer to "see" what is the state of requests.
A QTimer will post timer events in the creator thread's event loop.
Mostly, this is what makes QHttp asynchronous.
When a timer event occurs, QHttp will analyze in what state are it's requests.

The Qt event loop is just what it's name suggests.
It handles events posted by the event dispatcher ( QAbstractEventDispatcher ).
The event dispatcher will send events received from the underlying window system ,as well as from other components in your application, like other threads.

You should read about QApplication::exec, QCoreApplication::processEvents, QThread and everything you can find in the documentation about events, event loop and event handling.

Regards

fengtian.we
28th May 2007, 06:53
OK I will see , and thank you sir