PDA

View Full Version : Problem : use QHttp get a file to QFile



fengtian.we
24th May 2007, 09:16
:confused:

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)
}


why????

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

fengtian.we
24th May 2007, 09:18
I want use the file content, but I must finish the application for get a valid(have content) file .

wysota
24th May 2007, 09:48
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.

fengtian.we
24th May 2007, 09:56
OH~ so How can I get content from http://localhost/a2gold/index.php :)

jpn
24th May 2007, 10:02
See HTTP example (http://doc.trolltech.com/4.2/network-http.html).

fengtian.we
24th May 2007, 10:19
I see it yet.......... is loaded down with trivial details

jpn
24th May 2007, 10:26
#include <QtCore>
#include <QtNetwork>

int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
QFile file("index.php");
QHttp http("localhost");
http.get("/a2gold/index.php", &file);
app.connect(&http, SIGNAL(done(bool)), &app, SLOT(quit()));
return app.exec();
}

fengtian.we
24th May 2007, 10:39
... 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?

jpn
24th May 2007, 10:53
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.. :)

fengtian.we
24th May 2007, 10:58
haha thank you sir~ I will see the HTTP example more. :)