Results 1 to 9 of 9

Thread: How to use Qhttp for downloading?? with the following code..

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to use Qhttp for downloading?? with the following code..

    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

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Qhttp for downloading?? with the following code..

    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.

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use Qhttp for downloading?? with the following code..

    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.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Qhttp for downloading?? with the following code..

    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.

  5. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use Qhttp for downloading?? with the following code..

    Yes thats correct, How to make this done before my main event??

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Qhttp for downloading?? with the following code..

    Before? You were saying above that it was "at the final execution of the program".

    Which is it?

  7. #7
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use Qhttp for downloading?? with the following code..

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

  8. #8
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use Qhttp for downloading?? with the following code..

    Simplest way to download from the HTTP is below::

    Qt Code:
    1. connect(&http,SIGNAL(done(bool)),this,SLOT(httpdone())); //added in the constructor..
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QHttp http;
    2. QFile file;
    3.  
    4. private slots:
    5. bool on_pushButton_clicked();
    6. void httpdone();
    7.  
    8. signals:
    9. void done();
    10.  
    11. in the Qthttp.cpp file...
    12.  
    13. bool QtHttp::on_pushButton_clicked()
    14. {
    15.  
    16. QString strUrl="http://www.blabla.com//file";
    17. QUrl url = QUrl::fromUserInput(strUrl);
    18.  
    19. QFileInfo fileInfo(url.path());
    20. QString strhost=url.encodedHost();
    21. QString filename=fileInfo.fileName();
    22. file.setFileName("C:\\Qt\\QHttp\\"+filename);
    23. http.setHost(url.host(),url.port(80));
    24. http.get(url.path(),&file);
    25.  
    26. if(!file.open(QIODevice::WriteOnly))
    27. {
    28. QMessageBox::warning(NULL,"warning","file is not opened",QMessageBox::Ok);
    29. }
    30. file.write(http.readAll());
    31. http.close();
    32. return true;
    33.  
    34. }
    35.  
    36. void QtHttp::httpdone()
    37. {
    38.  
    39. file.close();
    40. Q_EMIT done();
    41. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th March 2011 at 12:52. Reason: missing [code] tags

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to use Qhttp for downloading?? with the following code..

    What are you trying to achieve? Is it some sort of "download a set of data before the main window shows" problem?

    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3. #include <QDebug>
    4.  
    5. class MainWindow: public QMainWindow {
    6. Q_OBJECT
    7. public:
    8. MainWindow(QWidget *p = 0): QMainWindow(p) {
    9. QWidget *central = new QWidget(this);
    10. QPushButton *button1 = new QPushButton("Button", this);
    11. QLabel *label1 = new QLabel("Label", this);
    12.  
    13. QVBoxLayout *layout = new QVBoxLayout(central);
    14. layout->addWidget(button1);
    15. layout->addWidget(label1);
    16.  
    17. central->setLayout(layout);
    18. setCentralWidget(central);
    19. }
    20. public slots:
    21.  
    22. private:
    23. };
    24.  
    25. class Downloader: public QObject {
    26. Q_OBJECT
    27. public:
    28. Downloader(QObject *p = 0): QObject(p) {
    29. manager = new QNetworkAccessManager(this);
    30. connect(manager, SIGNAL(finished(QNetworkReply*)),
    31. this, SLOT(replyFinished(QNetworkReply*)));
    32.  
    33. }
    34.  
    35. void start() {
    36. manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));
    37. }
    38. private slots:
    39. void replyFinished(QNetworkReply *reply) {
    40. // Save the received data
    41. qDebug() << "Saving the received data";
    42. //
    43. emit done();
    44. }
    45. signals:
    46. void done();
    47. private:
    48. QNetworkAccessManager *manager;
    49. };
    50.  
    51.  
    52. int main(int argc, char *argv[])
    53. {
    54. QApplication app(argc, argv);
    55.  
    56.  
    57. MainWindow m; // create but do not show()
    58. Downloader d;
    59. QObject::connect(&d, SIGNAL(done()), &m, SLOT(show()));
    60. d.start();
    61.  
    62. return app.exec();
    63. }
    64. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 18th March 2011 at 09:18.

Similar Threads

  1. About FTP uploading and downloading
    By prats in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2011, 11:28
  2. Downloading files
    By MTK358 in forum Newbie
    Replies: 3
    Last Post: 23rd June 2010, 00:08
  3. Replies: 11
    Last Post: 20th January 2009, 14:10
  4. Downloading from a clean url
    By travlr in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2007, 21:55
  5. QHTTP Status code
    By whoops.slo in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2006, 09:18

Tags for this Thread

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.