O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – Writing the OOP conform code using QSignalMapper

A more elegant way that does not rely on sender() would be to use QSignalMapper to receive the reply object in the argument of the slot. First, you need to add the QSignalMapper *m_imageFinishedMapper private field to your class. When you call QNetworkAccessManager::get() to request each image, set up the mapper as follows:

for(const QString& url: urls) {
    QNetworkRequest request(url);
    QNetworkReply *reply = m_network_manager->get(request);
    connect(reply, SIGNAL(finished()),
            m_imageFinishedMapper, SLOT(map()));
    m_imageFinishedMapper->setMapping(reply, reply);
}

In a prominent place, most likely the constructor of the class, connect the mapper's map() signal to a custom slot. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required