5ddb5d1fee
Creating a continuation with QtFuture::Launch::Async policy does not work well with the example, because it still needs to update the UI once the async continuation is finished. If the user decides to close the application while the async continuation is executed, the next continuation will be accessing data from the destroyed Images object. Fix it by using QtConcurrent::run() to do the "heavy" work in a separate thread, and use a QFutureWatcher to handle the results of the async execution. Update the example documentation accordingly. After this patch the example still shows the usage of continuations and onCanceled()/onFailed() handlers. However, it now does not illustrate the usage of different launch policies and continuation contexts. It might not be a big issue, because the QFuture documentation describes these topics rather extensively. Fixes: QTBUG-103514 Pick-to: 6.5 Change-Id: I8142535064ff7a4e8007a5c0a8fe7709d6d942ec Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
54 lines
1.2 KiB
C++
54 lines
1.2 KiB
C++
// Copyright (C) 2023 The Qt Company Ltd.
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
#ifndef IMAGESCALING_H
|
|
#define IMAGESCALING_H
|
|
|
|
#include <QtWidgets>
|
|
#include <QtConcurrent>
|
|
#include <QNetworkAccessManager>
|
|
#include <optional>
|
|
|
|
class DownloadDialog;
|
|
class Images : public QWidget
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
Images(QWidget *parent = nullptr);
|
|
~Images();
|
|
|
|
void initLayout(qsizetype count);
|
|
|
|
QFuture<QByteArray> download(const QList<QUrl> &urls);
|
|
void updateStatus(const QString &msg);
|
|
void showImages(const QList<QImage> &images);
|
|
void abortDownload();
|
|
|
|
public slots:
|
|
void process();
|
|
void cancel();
|
|
|
|
private slots:
|
|
void scaleFinished();
|
|
|
|
private:
|
|
//! [1]
|
|
using OptionalImages = std::optional<QList<QImage>>;
|
|
//! [1]
|
|
static OptionalImages scaled(const QList<QByteArray> &data);
|
|
|
|
QPushButton *addUrlsButton;
|
|
QPushButton *cancelButton;
|
|
QVBoxLayout *mainLayout;
|
|
QList<QLabel *> labels;
|
|
QGridLayout *imagesLayout;
|
|
QStatusBar *statusBar;
|
|
DownloadDialog *downloadDialog;
|
|
|
|
QNetworkAccessManager qnam;
|
|
QList<QSharedPointer<QNetworkReply>> replies;
|
|
QFuture<QByteArray> downloadFuture;
|
|
QFutureWatcher<OptionalImages> scalingWatcher;
|
|
};
|
|
|
|
#endif // IMAGESCALING_H
|