Results 1 to 10 of 10

Thread: Problem : use QHttp get a file to QFile

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

    Question Problem : use QHttp get a file to QFile



    My code:

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


    why????

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

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

    Default Re: Problem : use QHttp get a file to QFile

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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem : use QHttp get a file to QFile

    QHttp is an asynchronous class. When get() returns, the file is not yet downloaded but only scheduled for download. And it needs the event queue to work so before calling QApplication::exec() nothing will get downloaded.

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

    Default Re: Problem : use QHttp get a file to QFile

    OH~ so How can I get content from http://localhost/a2gold/index.php

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem : use QHttp get a file to QFile

    J-P Nurmi

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

    Default Re: Problem : use QHttp get a file to QFile

    I see it yet.......... is loaded down with trivial details

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem : use QHttp get a file to QFile

    Qt Code:
    1. #include <QtCore>
    2. #include <QtNetwork>
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7. QFile file("index.php");
    8. QHttp http("localhost");
    9. http.get("/a2gold/index.php", &file);
    10. app.connect(&http, SIGNAL(done(bool)), &app, SLOT(quit()));
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Default Re: Problem : use QHttp get a file to QFile

    ... I hope this:
    add a file path to a array.
    download it in the background.
    when a file is download finish,I read it and do something....

    can I?

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem : use QHttp get a file to QFile

    Quote Originally Posted by fengtian.we View Post
    I hope this:
    [...]
    can I?
    You have been given two examples. One slightly more advanced with GUI and yet another without GUI. The former is close to what you want. The latter is about as simple as it can get. What do you need more? We are not here to write your programs for you..
    J-P Nurmi

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

    Default Re: Problem : use QHttp get a file to QFile

    haha thank you sir~ I will see the HTTP example more.

Similar Threads

  1. QHttp and QFile not closing
    By maxpower in forum Qt Programming
    Replies: 2
    Last Post: 1st May 2007, 17:38
  2. QHttp Problem
    By musaulker in forum Newbie
    Replies: 4
    Last Post: 29th March 2007, 00:40
  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.