Dialog UI freezed/unresponsive until all the elements are finished loading into my Qlistwidget. Any way to solve this?

  • In my dialog i have qlistwidget and a "refresh-list" button, when i click refresh-list button this function

    Print_Descendants_key(exc.pUIAutomation, nullptr, 0);

    should run and it populates my qlistwidget with around 10000 items, but until this function prepares and adds 10000 items in my qlistwidget ,the UI remains frozen ,i am not able to see list-items being added sequentially but after a period of 2 mins of being unresponsive, it suddenly becomes perfectly responsive.

    How to tackle this ?? my desired effect is that,when this function is running , i should be able to see the elements adding up one-by-one/realtime in the QListwidget while the UI remains resonsive.

    Note (My try):- to acheive my desired effect, i have tried running that

    Print_Descendants_key(exc.pUIAutomation, nullptr, 0);

    function in a separate thread named "thread3" , and when i click refresh button, i was able to see the list-items being added in-front of me and the UI remained resposive, but i had a problem, when i clicked "Ok" ENTIRE UI CRASHES, i get errors in either qlist.h or abort.cpp

    The way i did it was like this---

    void Dialog::on_refreshbutton_clicked()
        std::thread thread3(&KeyComd::Print_step,this);
    void KeyComd::Print_step()
        Print_Descendants_key(exc.pUIAutomation, nullptr, 0);

  • Lifetime Qt Champion

    10.000 items take some time to create and add.

    Print_Descendants_key must not touch/access the ListWidget from the other thread. that will not be good.

    To make the UI less hanged while adding such a number of items, you can do 3 things.

    1: use a View + custom model instead.
    If you already have all the data in a list/vector, a custom model on top of that would be super fast.
    Use a thread to send data to main. Make sure the thread is not hammering the
    main gui as that would lag too. You can also use a QTimer and simply slow down the adding.
    Do the ugly trick of calling QApplication::processEvents() (in the loop that adds the items)
    to allow the main GUI to be more responsive.