Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]XML downloadfile
Forum Updated to NodeBB v4.3 + New Features

[Solved]XML downloadfile

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    toho71
    wrote on last edited by
    #1

    How can I wait to the file is completely downloaded and then read the file.
    i have tried this code but it doesn't do what I was hopeing for.

    @void MainWindow::test()
    {

    manager = new QNetworkAccessManager(this);
    reply = manager->get(QNetworkRequest(QUrl("http://www.w3schools.com/xml/simple.xml")));
    connect(reply,SIGNAL(finished()),SLOT(downloaded()));
    

    QXmlStreamReader reader( reply);
    int i=0;
    while (!reader.atEnd() && !reader.hasError())
    {
    if (reader.isStartElement())// && xml_stream.name() =="description")
    ui->textBrowser->append( QString::number(reader.lineNumber())
    + ":" + reader.readElementText());

        reader.readNext();
        i++;
    }
    

    qDebug() << i;
    }

    void MainWindow::downloaded()
    {
    QFile file("C:/testprojekt/filesXML/myTest.xml");
    file.open(QIODevice::WriteOnly);
    file.write(reply->readAll());
    file.close();
    reply->waitForReadyRead(1000);
    }@

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #2

      You have to put your parser in the downloaded slot as well if you really want to read the file once it's written.

      Get inspiration from here and [there])http://developer.qt.nokia.com/forums/viewthread/7425)

      I coded something similar to what (I guess) you want to achieve:

      void FileFetcher::setUrl( const QString& url )
      {
        Reply = Mgr -> get( QNetworkRequest( QUrl( url ) ) ); 
        connect( Reply, SIGNAL( finished() ), this, SLOT( downloaded() ) ); 
      }
      
      QIODevice * FileFetcher::networkReply() const
      {
        return Reply;
      }
      
      void FileFetcher::downloaded()
      {
      emit( fileFetched( Reply -> error() );
      }
      

      and I have another class which listens to the fileFetched signal:

      RequestMgr::RequestMgr( /* ... */ )
      {
      /*
      ...
      */
       connect( Fetcher, SIGNAL( fileFetched( QNetworkReply::NetworkError ) ),
          this, SLOT( readText( QNetworkReply::NetworkError ) ) );
      }
      
      void RequestMgr::readText( const QNetworkReply::NetworkError& statut )
      {
        if ( statut != QNetworkReply::NoError )
          {
            cerr << "An error occured while getting the file : "
          << statut << endl;
            return;
          }
        
          cout << "No problem in getting file" << endl;
        
        QIODevice * infos = Fetcher -> networkReply(); //
        
        XmlStreamReader * parser = new XmlStreamReader( infos );
      
      /*
      do whatever you want with the parser
      */
      }
      

      NB : I have a more complex class structure than what I presented here (moreover I had to translate variables and class names), this is not guaranteed to be 100% correct.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • T Offline
        T Offline
        toho71
        wrote on last edited by
        #3

        I'll try this and thank you again

        Works fine

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved