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. Reading an online file
Forum Updated to NodeBB v4.3 + New Features

Reading an online file

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.8k 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.
  • J Offline
    J Offline
    jocala
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jocala
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jocala
          wrote on last edited by
          #4

          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;
          

          }

          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andreyc
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jocala
              wrote on last edited by
              #6

              Thanks for the feedback.

              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