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. Filedownloader confusion
Qt 6.11 is out! See what's new in the release blog

Filedownloader confusion

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 4.4k 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.
  • D Offline
    D Offline
    Drj312
    wrote on last edited by
    #1

    I'm new to Qt and I'm trying to get data from a website. I'm trying to follow the Download Data from URL example with the filedownloader class.
    @
    url = "http://ichart.finance.yahoo.com/table.csv?s=bac";
    FileDownloader *m_webData = new FileDownloader(url,0);
    QByteArray webData= m_webData->downloadedData();
    qDebug() << webData << "webdata";
    @

    With the code like this, webData is just returned empty. I'm not sure what I'm missing.
    Thanks for any help.

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nicky j
      wrote on last edited by
      #2

      Do you mean its not actually downloading the file?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Drj312
        wrote on last edited by
        #3

        No its downloading the file.
        for example, if i add an output line to the following code, it shows the data I expect.
        @
        void FileDownloader::fileDownloaded(QNetworkReply* pReply)
        {
        m_DownloadedData = pReply->readAll();
        //emit a signal
        pReply->deleteLater();
        emit downloaded();
        }
        @
        I should have been more clear. The code from my first post is being executed in main (just for getting to know the class).
        so inside the class, the webdata is there. but when I try to get it from main, there is nothing there.

        the filedownloader class comes from http://qt-project.org/wiki/Download_Data_from_URL

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Code_ReaQtor
          wrote on last edited by
          #4

          I think it is because you called
          @QByteArray webData= m_webData->downloadedData();@
          immediately after the
          @FileDownloader *m_webData = new FileDownloader(url,0);@

          The data has not been downloaded yet. Try to create a delay between these lines. Or better, use signal-slot.

          Please visit my open-source projects at https://github.com/Code-ReaQtor.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Drj312
            wrote on last edited by
            #5

            thank you. I think you're right. I have no experience with asynchronous functions. It appears that is the issue. Do you have a link or example of using the signal slot for this?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Code_ReaQtor
              wrote on last edited by
              #6

              "Signals & Slots":http://qt-project.org/doc/qt-4.8/signalsandslots.html

              Please visit my open-source projects at https://github.com/Code-ReaQtor.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Drj312
                wrote on last edited by
                #7

                I read through that and unfortunately I have not figured out how to use it. I have a basic class where I have declared
                @
                FileDownloader *filedl = new FileDownloader(url,0);
                QByteArray info;
                @

                I'd like the downloaded data to go into info.
                I have function getData in my class that will read the data from the filedl.
                So I should have something like
                @connect(????, SIGNAL(downloaded()), SLOT(getData()));@
                Is that correct?

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  your FileDownloader insatnce triggers the signal downloaded(9 once the request is finished.

                  Thus do this:
                  @
                  m_Filedl = new FileDownloader(url,this);
                  connect(m_Filedl, SIGNAL(downloaded()), this, SLOT(onDownloadFinished()));
                  ...
                  void onDownloadFinished()
                  {
                  QbyteArray data = m_Filedl->downloadedData();
                  ....
                  }
                  @

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    Drj312
                    wrote on last edited by
                    #9

                    I am now getting the error:
                    error: C2660: 'connect' : function does not take 4 arguments

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Drj312
                      wrote on last edited by
                      #10

                      Ok so it works now! Thank you. The issue I was having before was that I was trying to connect the signal to main.cpp. I'm not exactly sure why, but that is wrong. I moved that code to the widget class definition and now it is working.

                      1 Reply Last reply
                      0
                      • raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #11

                        [quote author="Drj312" date="1390355433"]The issue I was having before was that I was trying to connect the signal to main.cpp. I'm not exactly sure why, but that is wrong.[/quote]
                        Because you used the "3-parameter-connect()":http://qt-project.org/doc/qt-4.8/qobject.html#connect-3, which automatically tries to connect it to the specified slot of the this-pointer.

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        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