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

QListWidget update issue



  • Hi,
    I'm trying to make simple layout for visualizing the sorting of vector at every step. For that I'm using a pushbutton which when clicked should sort the given input vector. To visualize this I added a delay in the for loop and also highlighting the item colour in the Widget whose position is changed.

        for (int i = 0; i < (tempVisVector.size() - 1); i++ )
             {
                ui->sortedArray->clear();
                double min = tempVisVector[i];
                unsigned int locat = i ;
    
                for (int j = i+1; j < (tempVisVector.size()); j++)
                {
                    if (min > tempVisVector[j])
                    {
                        min = tempVisVector[j];
                        locat = j;
                    }
                }
    
    
                //Swap the values
                double temp = tempVisVector[i];
                tempVisVector[i] = min;
                tempVisVector[locat] = temp;
    
                for (int p = 0; p < tempVisVector.size(); p++)
                {
                    qDebug()<<"Add Item";
                    ui->sortedArray->addItem(QString::number(tempVisVector[p]));
                }
                ui->sortedArray->item(i)->setBackground(Qt::red);
                ui->sortedArray->item(locat)->setBackground(Qt::green);
                qDebug()<<"Sleeping Time";
                Sleep(2000);
    
              }
    

    The added delay is working but my ListWidget does not get updated. It gets updated only at the end once the entire sorting is done. I'm not sure if the sleep () is obstructing this or not. ANy suggestions?



  • It is Working now without the use of QTimer. I removed the outer loop of sorting and calling it from on_click pushbutton slot. Thanks Guys!!


  • Qt Champions 2019

    @tsdk said in QListWidget update issue:

    ANy suggestions?

    No, it's correct. Qt can only redraw when it's back in the eventloop. Therefore don't block the eventloop with sleep() or similar.



  • @tsdk
    You absolutely will not want to use sleep(). You will want to use QTimer.



  • @Christian-Ehrlicher So any alternatives to display it otherwise I will have to use TableWidget.
    @JonB Will Try with QTimer.
    Thnx :)


  • Qt Champions 2019

    @tsdk said in QListWidget update issue:

    So any alternatives to display it

    Do one step, go back to the eventloop, do the next step. Maybe even with a QTimer so the user sees the changes otherwise it might go too fast.



  • It is Working now without the use of QTimer. I removed the outer loop of sorting and calling it from on_click pushbutton slot. Thanks Guys!!



  • @tsdk
    I don't know how you managed to achieve "visualization" [for the end user] without a QTimer. Sorting steps will complete so quickly that I can't believe the user will see it "animated" in progress, which I thought is what you wanted. But you seem to be happy :)


Log in to reply