Example: update imagescaling example

Updated the example to align with the Qt6 Example-Guideline.

https://wiki.qt.io/Qt6/Example-Guideline

Task-number: QTBUG-111165
Change-Id: Ibd9e7ce0d4dee90f6a693b81516d2f5b86345b1d
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit b5e9d418959a9d427c14468886470791527e157f)
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Dennis Oberst 2023-02-28 14:58:54 +01:00 committed by Ivan Solovev
parent c60e8ef680
commit 29f73f0c9a
5 changed files with 18 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

View File

@ -3,15 +3,17 @@
/*! /*!
\example imagescaling \example imagescaling
\title Image Scaling Example \meta tags {widgets, threads, network}
\brief Demonstrates how to asynchronously download and scale images. \title Image Scaling
\ingroup qtconcurrentexamples \ingroup qtconcurrentexamples
\image imagescaling_example.png \brief Demonstrates how to asynchronously download and scale images.
This example shows how to use the QFuture and QPromise classes to download a This example shows how to use the QFuture and QPromise classes to download a
collection of images from the network and scale them, without blocking the UI. collection of images from the network and scale them, without blocking the UI.
The application consists of the the following steps: \image imagescaling.webp
The application consists of the following steps:
\list 1 \list 1
\li Download images form the list of URLs specified by the user. \li Download images form the list of URLs specified by the user.
@ -148,4 +150,6 @@
The rest of the code is straightforward, you can check the example project for The rest of the code is straightforward, you can check the example project for
more details. more details.
\include examples-run.qdocinc
*/ */

View File

@ -4,14 +4,12 @@
#include "downloaddialog.h" #include "downloaddialog.h"
#include <QNetworkReply> #include <QNetworkReply>
#include <QtMath>
#include <qmath.h>
#include <functional> #include <functional>
Images::Images(QWidget *parent) : QWidget(parent), downloadDialog(new DownloadDialog(this)) Images::Images(QWidget *parent) : QWidget(parent), downloadDialog(new DownloadDialog(this))
{ {
setWindowTitle(tr("Image downloading and scaling example"));
resize(800, 600); resize(800, 600);
addUrlsButton = new QPushButton(tr("Add URLs")); addUrlsButton = new QPushButton(tr("Add URLs"));
@ -90,7 +88,7 @@ void Images::process()
// Abort all pending requests // Abort all pending requests
abortDownload(); abortDownload();
}) })
.onFailed([this](const std::exception& ex) { .onFailed([this](const std::exception &ex) {
updateStatus(tr(ex.what())); updateStatus(tr(ex.what()));
}); });
//! [6] //! [6]
@ -117,7 +115,7 @@ QFuture<QByteArray> Images::download(const QList<QUrl> &urls)
//! [9] //! [9]
//! [10] //! [10]
for (auto url : urls) { for (const auto &url : urls) {
QSharedPointer<QNetworkReply> reply(qnam.get(QNetworkRequest(url))); QSharedPointer<QNetworkReply> reply(qnam.get(QNetworkRequest(url)));
replies.push_back(reply); replies.push_back(reply);
//! [10] //! [10]
@ -142,10 +140,10 @@ QFuture<QByteArray> Images::download(const QList<QUrl> &urls)
promise->finish(); promise->finish();
} }
//! [12] //! [12]
}).onFailed([=] (QNetworkReply::NetworkError error) { }).onFailed([promise] (QNetworkReply::NetworkError error) {
promise->setException(std::make_exception_ptr(error)); promise->setException(std::make_exception_ptr(error));
promise->finish(); promise->finish();
}).onFailed([=] { }).onFailed([promise] {
const auto ex = std::make_exception_ptr( const auto ex = std::make_exception_ptr(
std::runtime_error("Unknown error occurred while downloading.")); std::runtime_error("Unknown error occurred while downloading."));
promise->setException(ex); promise->setException(ex);
@ -164,7 +162,7 @@ QList<QImage> Images::scaled() const
{ {
QList<QImage> scaled; QList<QImage> scaled;
const auto data = downloadFuture.results(); const auto data = downloadFuture.results();
for (auto imgData : data) { for (const auto &imgData : data) {
QImage image; QImage image;
image.loadFromData(imgData); image.loadFromData(imgData);
if (image.isNull()) if (image.isNull())

View File

@ -1,15 +1,16 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtWidgets>
#include <QtConcurrent>
#include "imagescaling.h" #include "imagescaling.h"
#include <QApplication>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc,argv); QApplication app(argc,argv);
app.setOrganizationName("QtProject");
app.setApplicationName(QObject::tr("Image Downloading and Scaling"));
Images imageView; Images imageView;
imageView.setWindowTitle(QObject::tr("Image Downloading and Scaling"));
imageView.show(); imageView.show();
return app.exec(); return app.exec();