Solved 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!!
-
@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 usesleep()
. You will want to useQTimer
. -
@Christian-Ehrlicher So any alternatives to display it otherwise I will have to use TableWidget.
@JonB Will Try with QTimer.
Thnx :) -
@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 aQTimer
. 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 :)