Question: I implemented a widget/dialog, which is just a gif animation inside (CircularProgressDlg). Our application consists of Login dialog part and MainWindow part. Between login is accepted and MainWindow is fully loaded there are 5 to 10 seconds of huge init() function.

What I need is to launch my CircularProgressDlg when login is accepted, and I need it to animate it's gif non-blocking. I can't use "update" or "processEvents" inside this huge "init" function, cause it is very complex inside and it won't be smooth enough.

It looks very needful that this type of dialogs, detached from main event loop, exist! So how to achieve that?
I understand that I can create a detached Process with this CircularProgressDlg, but let's leave this way to the end.

What are other ways?
We use QtWidgets, but maybe I can implement small QML dialog for such cases if, of course, QML will solve my problem. Will it?
Which options do I have?

Answer: 
Use the Qt Concurrent library, specifically its [`QConcurrent::run` method](https://doc.qt.io/qt-5/qtconcurrentrun.html#running-a-function-in-a-separate-thread) to spawn a thread for your expensive task. You get back a [`QFuture`](https://doc.qt.io/qt-5/qfuture.html) that you can watch with [`QFutureWatcher`](https://doc.qt.io/qt-5/qfuturewatcher.html).

The QT documentation has an [asynchronous image scaling example](https://code.qt.io/cgit/qt/qtbase.git/tree/examples/qtconcurrent/imagescaling?h=5.14).
I'm adding selected parts below.

* Creating a QFutureWatcher to be signaled when a future is done:

```
    imageScaling = new QFutureWatcher<QImage>(this);
    connect(imageScaling, &QFutureWatcher<QImage>::resultReadyAt, this, &Images::showImage);
    connect(imageScaling, &QFutureWatcher<QImage>::finished, this, &Images::finished);

```

* Constructing the future:

```
    std::function<QImage(const QString&)> scale = [imageSize](const QString &imageFileName) {
        QImage image(imageFileName);
        return image.scaled(QSize(imageSize, imageSize), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    };
    QFuture<QImage> fut = QConcurrent::run(scale, "test.png");

```

* Attaching the future to the futurewatcher:

```
    imageScaling->setFuture(fut);

```
Thanks to everyone. We decided to make things right and refactor loading and updating our widgets in a non-blocking way. With moving all possible logic in separate processes.