Reading an online file



  • I'm trying to implement a simple version checker below. I'm getting a zero error code reading the file, but the file contents show up blank. You can access the file via browser, permissions are ok.

    @
    void check_version()
    {
    QNetworkAccessManager *nam = new QNetworkAccessManager();
    QUrl data_url("http://www.example.com/version.txt");
    QNetworkRequest req(data_url);
    QNetworkReply *reply = nam->get(req);
    QString data = reply->readAll() ;
    QString newver(data);
    int err = reply->error();
    QString s = QString::number(err);
    QMessageBox::critical(0, "",newver+" "+s,QMessageBox::Cancel);
    }
    @



  • Reply is not immediately available after GET.
    Take a look on "Network Download Example":http://qt-project.org/doc/qt-5/qtnetwork-download-example.html



  • Thanks. That's a lot of code.

    I gather the problem is that I need to wait to read until the get is finished, so I need a signal and a slot: the signal tells the slot to read the data.

    @
    QObject::connect(&rep, SIGNAL( rep is finished ),
    QByteArray newver , SLOT( reply->readAll() ));
    @

    I know the pseudocode above is bogus, thanks for helping me understand.



  • Here's what I ended up with. I hope this working example helps someone else. If there's anythng wrong or it can be improved, please post.

    @
    void MainWindow::get_data() {
    QNetworkRequest request;
    request.setUrl(QUrl("http://www.example.com/version.txt"));

    QNetworkAccessManager *nam = new QNetworkAccessManager();
    QNetworkReply *reply = nam->get(request);

    connect(reply, SIGNAL(finished()),
    this, SLOT(onReqCompleted()));

    if (reply->error() != QNetworkReply::NoError)
    {

       int err = reply->error();
       QString s2 = QString::number(err);
       QMessageBox::critical(0, "","Network error: " + s2,QMessageBox::Cancel);
    

    }

    }

    void MainWindow::onReqCompleted() {
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    QByteArray data = reply->readAll();

    QString s1(data);
    s1 = strip(s1);
    int err = reply->error();
    QString s2 = QString::number(err);

       if (version != s1)
       {
           QMessageBox::StandardButton reply;
            reply = QMessageBox::question(0, "", "Version "+s1+" is ready. Download?",
                    QMessageBox::Yes|QMessageBox::No);
              if (reply == QMessageBox::Yes)
               {
                  QString link = "http://www.example.com/file.html";
                  QDesktopServices::openUrl(QUrl(link));
                }
    
       }
    
       delete reply;
    

    }

    @



  • Thank you for sharing your solution.

    Comment:
    You don't need to check a reply->error() in MainWindow::get_data()
    It should be in MainWindow::onReqCompleted() before reply->readAll()



  • Thanks for the feedback.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.