Important: Please read the Qt Code of Conduct -

Filedownloader confusion

  • 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 = "";
    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.

  • Do you mean its not actually downloading the file?

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

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

  • 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?

  • 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?

  • Moderators

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

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

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

  • Moderators

    [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()":, which automatically tries to connect it to the specified slot of the this-pointer.

Log in to reply