2

i'm trying to load some data from a server and fill a Qt list. i want to run the dowloanding in a thread. so there is the code of:

  • principal function in the App.cpp

    loadInterestPlaces(QString& urlInterestPlaces) {
    LoadData* Data = new LoadData(urlInterestPlaces);
    
    QFuture< list <InterestPlace *> > future = QtConcurrent::run(Data,
            &LoadData::startLoading);
    
    
    
    // Invoke our onLoadingFinished slot after the loading has finished.
    bool ok = connect(&m_watcher, SIGNAL(finished()), this,
            SLOT(onLoadingFinished()));
    Q_ASSERT(ok);
    Q_UNUSED(ok);
    
    // starts watching the given future
    m_watcher.setFuture(future);
    
    }
    
    void ApplicationUI::onLoadingFinished() {
    qDebug() << "Loading finished";
    
    
    interestPlacesList = m_watcher.future().result();
    qDebug() << "List size= " << interestPlacesList.size();
    
        }
            }
    
  • the LoadData.cpp file : this is the code of the startloanding function :

    std::list<InterestPlace *> LoadData::startLoading()
    {
    QNetworkAccessManager* netManager = new QNetworkAccessManager(this);
    
    const QUrl url(_URL);
    QNetworkRequest request(url);
    
    QNetworkReply* reply = netManager->get(request);
    
    netManager->moveToThread(this->thread());
    netManager->setParent(this);
    
    bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished()));
    qDebug() << reply->isFinished();
    Q_ASSERT(ok);
    Q_UNUSED(ok);
    
    qDebug() << "load data: liste size" <<interestPlacesList.size();
    return interestPlacesList;
    }
    

Finally inside the SLOT onreplyfinished i parse the data and fill the list. But the problem here, the QFuture is finished before the downloading so that the list is always empty. How could i return the list filled just after the execution of the onReplyFinished ?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
oumaimadev
  • 69
  • 7
  • `netManager->moveToThread(this->thread());` is only valid when netManager is already in this thread which will make it a no-op – ratchet freak Nov 18 '13 at 11:06
  • also what is stopping you from having `onReplyFinished` trigger `onLoadingFinished` – ratchet freak Nov 18 '13 at 11:07
  • problem here is not QtConcurrent but `LoadData::startLoading()`. Without QtConcurrent it will also not work. So first rethink what exactly and how you are trying to do this. – Marek R Nov 18 '13 at 11:50

1 Answers1

0

You may be making this more complex than you need to. Here is what I do to get data off the web. In my case I'm downloading a large ZIP file, writing it out and then unziping it, but the basic steps are the same (I have omitted a lot of my specific code for clarity):

void HtmlDownloader::startDownload() {
    // Creates the network request and sets the destination URL
    QNetworkRequest request = QNetworkRequest();
    request.setUrl(mUrl);

    // Creates the network access manager and connects a custom slot to its
    // finished signal. Checks the return value for errors.
    QNetworkAccessManager *networkAccessManager =
            new QNetworkAccessManager(this);
    bool c = connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(requestFinished(QNetworkReply*)));

    Q_ASSERT(c);

    // Indicate that the variable res isn't used in the rest of the app, to prevent
    // a compiler warning
    Q_UNUSED(c);

    // Sends the request
    QNetworkReply *reply = networkAccessManager->get(request);

    c = connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64)));
    Q_ASSERT(c);

}

void HtmlDownloader::requestFinished(QNetworkReply *reply) {
// Handle the reply data...
// Check the network reply for errors
    if (reply->error() == QNetworkReply::NoError) {
        // use reply->readAll() or reply->read() to get the returned data
        // and process into a list. The list can't be returned but could
        // be sent as a payload of a signal, or stored in a data member and
        // a signal sent to indicate it is available.
    }
    reply->deleteLater();
}

void HtmlDownloader::onDownloadProgress(qint64 bytesRecieved, qint64 bytesTotal) {
    emit downloadProgress(bytesRecieved, bytesTotal);
}
Richard
  • 8,920
  • 2
  • 18
  • 24