Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Non-blocking progress dialog during huge amount of non-controllable calculations.



  • 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?


  • Lifetime Qt Champion

    @IsAnton Your progress dialog will be in the same process. I think you mix some things here: a process is an application in execution. If this application shows a progress dialog it is still same process.
    "logic is very complex and mixed with Gui interaction" - well, it should be still possible to move this logic into another thread. You just need to make sure that this thread does not access the UI directly. Instead emit signals in that thread when needed and react on this signals in main thread.


  • Lifetime Qt Champion

    Hi,

    As soon as you have activities that blocks the event loop, you won't be able to do what you want. Move the processing to a different thread or use a static mean of showing that your application is busy.



  • @SGaist Yes, you are right Event loop is blocked. Ok, I understand, I have to create Process, if I need animated progress. Will see other recommendations then will close topic.


  • Lifetime Qt Champion

    @IsAnton said in Non-blocking progress dialog during huge amount of non-controllable calculations.:

    I have to create Process

    Thread not process most likely



  • @jsulm I can't create just Thread and move huge pending logic there, logic is very complex and mixed with Gui interaction, it has to be in main thread. I mean exactly the Process, in which a different event loop will exist, for my progress dialog.


  • Lifetime Qt Champion

    @IsAnton Your progress dialog will be in the same process. I think you mix some things here: a process is an application in execution. If this application shows a progress dialog it is still same process.
    "logic is very complex and mixed with Gui interaction" - well, it should be still possible to move this logic into another thread. You just need to make sure that this thread does not access the UI directly. Instead emit signals in that thread when needed and react on this signals in main thread.



  • @jsulm Codebase of our initialization is quite big, I would need weeks or even more to refactor it correctly, to separate server interactions and GUI update.
    I am just thinking of an easy way to create nonblocking dialog on top of main event loop.
    Look in https://doc.qt.io/qt-5/qeventloop.html there is a description:
    "At any time, you can create a QEventLoop object and call exec() on it to start a local event loop. From within the event loop, calling exit() will force exec() to return."

    It looks like I can create another event loop and put my Dialog there! Is it real?


  • Lifetime Qt Champion

    @IsAnton I don't see how creating a second event loop will help you.
    https://doc.qt.io/qt-5/qeventloop.html#exec is a blocking call! As soon as you call it in your initialisation code you will block your initialisation code.
    What could work (but is an ugly work-around) is to make sure you call https://doc.qt.io/qt-5/qcoreapplication.html#processEvents regularly and often enough in your initialisation code to give the event loop time to process events.


  • Moderators

    @IsAnton
    what you can to is quite literally an other process.

    Or more precise an other QApplication that consists only out of a waiting animation.

    I do that in one of my Applications.

    I have 2 executables a splash screen and the main app. On start up I start the splashscreen via QProcess and after initialization of the main App I kill the process

    Only downside, depending on the OS you may end up with 2 Icons in your hotbar.



  • @J-Hilk @jsulm @SGaist Thanks to everyone. We decided to make things right and refactor loading and updating our widgets in a non-blocking way.


Log in to reply