Results 1 to 7 of 7

Thread: QHTTP problem,download multi-file.

  1. #1
    Join Date
    Feb 2007
    Posts
    71
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question QHTTP problem,download multi-file.

    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??

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHTTP problem,download multi-file.

    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
    Last edited by marcel; 27th May 2007 at 12:20.

  3. #3
    Join Date
    Feb 2007
    Posts
    71
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QHTTP problem,download multi-file.

    My code:

    Qt Code:
    1. #include <QUrl>
    2. NOTE: I run it step to step ( VS2005 debug function ).
    3. #include <QtGui/QApplication>
    4. #include <QHttp>
    5. #include <QFile>
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication *app=new QApplication(argc, argv);
    9. QFile *file=new QFile("index.php");
    10. if(!file->open(QIODevice::WriteOnly)) //file create
    11. { QMessageBox::information(0,"Get User's Profile","Error: Cannot open["+file->fileName()+"] "+file->errorString());
    12. return 0;
    13. }
    14. QUrl *url=new QUrl("http://localhost/a2gold/index.php");
    15. QHttp *http=new QHttp(url->host());
    16. http->get(url->path(),file);
    17. http->close();
    18. file->flush(); //Now,file is empty
    19. return app->exec(); //since there,file is valid(have content)}
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by fengtian.we; 27th May 2007 at 16:16.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHTTP problem,download multi-file.

    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

  5. #5
    Join Date
    Feb 2007
    Posts
    71
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QHTTP problem,download multi-file.

    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???

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHTTP problem,download multi-file.

    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

  7. #7
    Join Date
    Feb 2007
    Posts
    71
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QHTTP problem,download multi-file.

    OK I will see , and thank you sir

Similar Threads

  1. File Binary Upload QHttp find the bug/s
    By patrik08 in forum Qt Programming
    Replies: 13
    Last Post: 10th June 2008, 07:51
  2. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  3. Retrieving modified date of file using QHttp
    By hardgeus in forum Qt Programming
    Replies: 9
    Last Post: 3rd December 2006, 23:20
  4. QHttp "PUT METHOD" QT Problem File Upload. RFC 2616
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2006, 22:02
  5. QHttp GET File & Password
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 11th June 2006, 13:04

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.